Extract the value of a array from a function

period (t1, t2, t3) = time (p)
where t1 , t2, t3 are n dimensional arrays say
a = (1,3,4,6,8,9,0,5,4,)
b = (3)
c = (4,56,7,8,5,1)
t1 = length(a)
t2 = length(b)
t1 = length(c)
Since I have a function called period it gives some random value based on the parameters that have been passed to t1, t2, t3.
output -- period (5,1,2) = 12.
what I'm trying is to do trace it back
i have time as 12, just by looking at the output i want to trace the value of t1 that has been passed.
I want the 5 element of t1 as the result, thats 8
How do i get it ?
kindly help

2 Commenti

Does the function period only accept integer inputs? Is it possible to write its inverse based on its definition? Can you show us the code of the period function?
Yes, it will be only integers.
how do i write the inverse of it ?
Please let me know

Accedi per commentare.

Risposte (1)

In case the function period is a block-box or cannot be inverted, then you can an optimization-based approach to find its inverse. For example, if the input to the function period can be any real number
obf_fun = @(x) (period(x(1),x(2),x(3)) - 12).^2;
sol = fmincon(obf_fun, rand(1,3));
a = sol(1);
b = sol(2);
c = sol(3);
If the function period can only accept integer inputs, then you will need global optimization toolbox to find the solution
obf_fun = @(x) (period(x(1),x(2),x(3)) - 12).^2;
sol = ga(obf_fun, 3, [], [], [], [], [], [], [], 1:3)
a = sol(1);
b = sol(2);
c = sol(3);

57 Commenti

Ganesh Kini
Ganesh Kini il 5 Apr 2020
Modificato: Ganesh Kini il 5 Apr 2020
Is it possible to explain in a few words?
the time can be in decimal too for example time 12.18, but the inputs to the period will be integers.
If the time is a decimal number, can i trace it back?
Can you please let me know
Ganesh, if the inputs to period are integers, then you will need to use the second code, i.e., ga() algorithm. Yes, the time can be fractional too. You can replace 12 with a decimal number.
Explanation: We have defined objective functions like this
Which will search for the value of a, b, and c which will minimize the value of the . As you can see, when this value is minimized, the output of the period will be the same as time. The algorithm will output the value of a,b, and c, which brings the output of the period closest to the time value.
Thank you for the help
But my query was regarding getting the value of the 5th element of t1 based on the time.
I guess the above code minimises and then gets the value closer to the time = 12
Ganesh, based on the information provided in your question, it is impossible to get the 5th element of vector a. That information about vector a was not used by the function period, and therefore lost. If there is some other information available about how to construct vector a from the value of t1 then it might be possible.
Ganesh Kini
Ganesh Kini il 5 Apr 2020
Modificato: Ganesh Kini il 5 Apr 2020
a = (1,3,4,6,8,9,0,5,4,)
b = (3)
c = (4,56,7,8,5,1)
a, b,c are saved in .vec extension
for t1 =1:1:length(a)
for t2=1:1:length(b)
for t3=1:1:length(c)
period_fun(a,b,c)=time(p);
p=p+1;
end
end
end
So this code will give me the time
period fun(5,1,3) which means it will fetch
5th element of a = 8, 1st element of b = 3, 3rd element of c = 7, and the output is time = 1.1
Now just by looking the output time 1.1 i want to extract the 5th element of a or the 1st element of b or 3rd element of c and get that element as output
is it possible ?
Can you show your actual code. This code does not make much sense. what is the value of variable p? what is the value of variable time(p)? How thw vectors a, b, and c are loaded?
Ganesh Kini
Ganesh Kini il 5 Apr 2020
Modificato: Ganesh Kini il 5 Apr 2020
a =load('numbers.vec');
b =load('numbers1.vec');
c =load('numbers2.vec');
the file number.vec = (1,3,4,6,8,9,0,5,4) and so on
time (p) is just the index
this is a part of the output for time(p)
12.7271
6.0866
6.0278
3.7492
9.181100000000001
5.2952
13.8514
6.918
6.1957
4.113
11.8323
5.4656
7.3774
5.3561
13.4326
6.7934
6.3158
4.1806
9.674099999999999
6.0949
14.502
1.1
So i will choose a random time i.e 1.1 and i need to get the 5 th element of a
I hope you understood, Please let me know
If you have time vector like give above, then you can do something like this
p = find(time == 1.1);
it will give you the value of p. But you didn't explain how the value of p increase with the values of t1, t2, and t3 inside the for loops.
p = p+1
it increases by one step after the computation of one set.
this will work if the data is arranged in a matrix. I need to access the file directly
If it is in a text file, then you cannot access it directly in MATLAB. If it is saved in .mat file, then you can directly access in on your disk without loading into MATLAB.
it is saved in .mat
how do i access it ? Does the above solution work to extract data which is in a array and not a matrix
It will directly access variables from the disk without loading them into memory. For matrices, it can be used with a slight modification. Is p stored as matrix? If yes, what are its dimensions?
Hi,
Could you please help me with other source. I tried it is not working
Ganesh, you will need to share your files and current code so that I can easily suggest a solution.
Hi Ameer,
after looking at the existing code and analysis.
period_fun(a,b,c)=time(p);
period_fun is not a function, its just an array.
Could you please let me know how to access the elements passed to it ?
From the limited information I have, i can only suggest this
idx = find(period_fun == 1.1); % suppose your are looking for 1.1 in period_fun
[a,b,c] = ind2sub(size(period_fun), idx);
Hi Ameer,
idx = find(period_fun == 1.1)
output of idx -- 1
this will find the index of the time, which does not serve the purpose tracing back the elements that are passed
[a,b,c] = ind2sub(size(period_fun), idx);
And finding 1.1 in period_fun does not make sense
please correct me if im wrong
The line
idx = find(period_fun == 1.1)
does not give an index in time. It gives a linear index in the matrix period_fun. The line
[a,b,c] = ind2sub(size(period_fun), idx)
convert the linear index into the values of subscripts a, b, and c
Ganesh Kini
Ganesh Kini il 7 Apr 2020
Modificato: Ganesh Kini il 7 Apr 2020
Sorry for the last comment. It was my mistake
so my code
value = period_fun(2,1,1,10,10,15,3);
Disp(value) gives me 1.1
now when i run
idx = find(period_fun == 1.1)
disp(idx)-- 62988
i tried looking for what it is, didnt get a clue
could you please tell what 62988 indicate ?
62988 is the linear index. To convert into a,b, and c you need to use ind2sub. See the difference between linear indexing and Subscripts indexing on this page: https://www.mathworks.com/company/newsletters/articles/matrix-indexing-in-matlab.html
Hi,
i have passed something like
value = period_fun(2,1,1,10,10,15,3); i.e. value(a,b,c,d,e,f,g,h) it should be a 7D matrix.
but here its 62988. Can you help me here ?
You can get the value of a,b,...h back using
[a,b,c,d,e,f,g,h] = ind2sub(size(period_fun), 62988);
Hi Ameer,
Sorry for the late reply, the solution works.
But suppose the output is 1.0 instead 1.1
the closest value of 1.0 is obviously 1.1 ?
But how do i do that ? How do i map the closest value based on a random output ?
Can you please guide me
Ameer Hamza
Ameer Hamza il 16 Apr 2020
Modificato: Ameer Hamza il 16 Apr 2020
Use round() function.
Hi,
I guess you didnt get my query.
I have the output as 1. I have to find the nearest value of 1 (i.e 1.1)
How do i do that ?
You can try something like
[~,idx] = min(abs(time-1));
idx is the index of 1.1 in time
time(idx) % output should be 1.1
Hi
The above code is not working
I have tried this before
Can you suggest some other alternative ?
Can you attach variable time in a .mat file? It will make it easier to sugges a solution.
Hi, Please forget the previous part. I will explain here
period = period_temp(2,1,1,4,5,6,1)+0.3;
the output is period = 11.5
I have an array period_temp (a,b,c,d,e,f,g) which gives a lot of values in 2 * 1 * 1 * 10 * 10 *10* 8 matrix
but 11.5 doesnt exist in the matrix since we i have added 0.3 as constant to period_temp.
Now How do I find the closest value to 11.5?
I have tried with
idx = interp1(period_temp, 1: length (period_temp), period, 'nearest');
Output: error: y(168000,_): but y has size 15x1
Please suggest
See this example:
x = rand(2,2,2);
val = 0.5;
[~,idx] = min(abs(x-val), [], 'all', 'linear');
[i1,i2,i3] = ind2sub(size(x), idx); % return index in each dimension
closest_value = x(i1,i2,i3);
Hi
The solutuion is not working
Please suggest some other solution
error warning: print_usage: Texinfo formatting filter exited abnormally
warning: called from
print_usage at line 74 column 5
warning: print_usage: raw Texinfo source of help text follows...
error: Invalid call to min. Correct usage is:
error: called from
print_usage at line 91 column 5
Is this the complete error message shown by MATLAB?
I am actually doing it Octave, which is similar to matlab
The solution I suggest works in MATLAB but not I octave, so there are some fundamental differences between two. You should try to convert my method to octave according to its function.
I have already posted an answer to that question.
Hi Ameer,
I am facing one issue here
idx = find(period_fun == 1.1); % suppose your are looking for 1.1 in period_fun
[a,b,c] = ind2sub(size(period_fun), idx);
So when i run it. sometimes a carries more than one index
Suppose for eg for the code when i run it it get
a = 1.000 4.0000
Expected answer is 1.000 but why did 4.0000 come ?
How can we eliminate the other values?
Please help me out
PS: FYI the array of a = (1,3,4,6,8,9,0,5,4,)
two values of a mean that 1.1 exist at two location in period_fun. You can get a single value like this
idx = find(period_fun == 1.1, 1);
1.1 is unique. a,b,c combination will be unique
but a is taking two or three values from its array.
Can you please help me in this ?
If 1.1 is uniuq, then a cannot take more then one value. Can you show an example where this happen?
abc = period_fun(2,1,2,5,5,13,8);
%finding the nearest possible value
dist = abs(period_fun - abc);
[min_dist, idx] = min(dist(:));
nearestvalue = period_fun(idx);
actualidx= find(period_fun ==nearestvalue);
[p1,p2,p3,p4,p5,p6,p7] = ind2sub(size(period_fun), actualidx);
v1 = nw_vec(p5);
First Case
v1 is displaying as follows
0.80000 0.65000 0.75000
all these are in the nw_vec array but 0.8 is expected answer. I should get only 0.8 as the answer
Second case
v1 is displaying as follows
0.40000 0.55000
0.2 is expected but its not giving it as output. I should get only 0.2 as the answer
How can i solve this ?
How is nw_vec defined?
Ganesh Kini
Ganesh Kini il 27 Apr 2020
Modificato: Ganesh Kini il 27 Apr 2020
nw_vec is .vec file
the file is as follows
0.2, 0.36, 0.38, 0.40, 0.47, 0.5, 0.55, 0.65, 0.75, 0.8
You are just using p5, why are you not using all the indexes: p1,p2,p3,p4,p5,p6,p7?
Ganesh Kini
Ganesh Kini il 27 Apr 2020
Modificato: Ganesh Kini il 27 Apr 2020
Because i want only p5.
abc = period_fun(2,1,2,5,5,13,8) takes the form of abc = period_fun(ioc,ipc,irc,inw,ivw,ivdp,itp);
where ivw=1:1:length(nw_vec) and so on for all the ioc,ipc,irc,inw,ivw,ivdp,itp.
We have 7 arrays that has been passed to period_fun
and from abc we are tracing it back
I want only values from nw_vec
You will need to share period_fun values to diagnose the issue.
No value in period_fun is 1.1. How can it find this value?
Ganesh Kini
Ganesh Kini il 27 Apr 2020
Modificato: Ganesh Kini il 27 Apr 2020
Yes, i just gave it as example.
let me give you the results from the output from my screen
abc = period_fun(2,1,2,5,5,13,8)
abc= 2.3918
and after the above code i should get
v1 = nw_vec(p5);
v1 should be 0.47 ( Only for this combination)
But at the output i am getting 0.47, 0.65
I should get only 0.47 as the answer
When I run this in MATLAB. It just get single value (0.8)
fid = fopen('Mat file.txt');
data = textscan(fid, '%f', 'HeaderLines', 6);
period_arr = data{1};
fclose(fid);
period_arr = padarray(period_arr, 168000 - numel(period_arr), 0, 'post');
period_arr = reshape(period_arr, [2 7 1 10 10 15 8]);
abc = 2.3918;
nw_vec = [0.2, 0.36, 0.38, 0.40, 0.47, 0.5, 0.55, 0.65, 0.75, 0.8];
dist = abs(period_arr - abc);
[min_dist, idx] = min(dist(:));
nearestvalue = period_arr(idx);
actualidx = find(period_arr == nearestvalue);
[p1,p2,p3,p4,p5,p6,p7] = ind2sub(size(period_arr), actualidx);
v1 = nw_vec(p5);
Result:
>> idx
idx =
37747
>> nearestvalue
nearestvalue =
2.3918
>> actualidx
actualidx =
37747
>> p5
p5 =
10
>> v1
v1 =
0.8000
period_arr(2,1,1,9,9,13,8);
can you try this combination ??
Just to cross verify
It get a single value (0.75) again
fid = fopen('Mat file.txt');
data = textscan(fid, '%f', 'HeaderLines', 6);
period_arr = data{1};
fclose(fid);
period_arr = padarray(period_arr, 168000 - numel(period_arr), 0, 'post');
period_arr = reshape(period_arr, [2 7 1 10 10 15 8]);
abc = period_arr(2,1,1,9,9,13,8);
nw_vec = [0.2, 0.36, 0.38, 0.40, 0.47, 0.5, 0.55, 0.65, 0.75, 0.8];
dist = abs(period_arr - abc);
[min_dist, idx] = min(dist(:));
nearestvalue = period_arr(idx);
actualidx = find(period_arr == nearestvalue);
[p1,p2,p3,p4,p5,p6,p7] = ind2sub(size(period_arr), actualidx);
v1 = nw_vec(p5);
Result:
>> v1
v1 =
0.7500
But I am getting that way, what can be the potential reason ?
are you running it in MATLAB?
Ganesh Kini
Ganesh Kini il 27 Apr 2020
Modificato: Ganesh Kini il 27 Apr 2020
for ioc=1:1:length(ring_vec)
for ipc=1:1:length(opcon_vec)
for irc=1:1:length(rccorner_vec)
for inw=1:1:length(nw_vec)
for ivw=1:1:length(pw_vec)
for ivdp=1:1:length(vd_vec)
for itp=1:1:length(temp_vec)
abc = period_fun(ioc,ipc,irc,inw,ivw,ivdp,itp);
%finding the nearest possible value
dist = abs(period_fun - abc);
[min_dist, idx] = min(dist(:));
nearestvalue = period_fun(idx);
actualidx= find(period_fun ==nearestvalue);
[p1,p2,p3,p4,p5,p6,p7] = ind2sub(size(period_fun), actualidx);
v1 = nw_vec(p5);
end
end
end
end
end
end
This is the overall program, do you think i need to refresh or clear the variables ? Yes in MATLAB
or is it even possible to tweak/ change the code a little bit, so that we get the accurate results
I cannot see any reason why you are getting two values in v1. The most I can suggest us to use a breakpoint and run your code line by line. Also, have you tried to find() with second input as I mentioned in one of my previous comment
actualidx= find(period_fun ==nearestvalue, 1);
Ganesh Kini
Ganesh Kini il 28 Mag 2020
Modificato: Ganesh Kini il 28 Mag 2020
Hi Ameer,
Thanks for the help
t1 ´= 2.3
t2 = 5.6
%its is a 2*7*1*10*10*15*8 matrix
p = period_arr(1,:,:,:,:,:,:); % this is of the form p = period_arr(i1, i2, i3, i4, i5, i6, i7);
n = period_arr(2,:,:,:,:,:,:); % this is of the form n = period_arr(i1, i2, i3, i4, i5, i6, i7);
dist_p = abs(p - t1);
[min_dist_p, idx_p] = min(dist_p(:));
dist_n = abs(n - t2);
[min_dist_n, idx_n] = min(dist_n(:));
c_tp = p(idx_p);
c_tn = n(idx_n);
So based on the minimum distance i get the closest value, and it is working fine.
But, I have a problem here I have to get only the closest value where the indices i4 of p = i4 of n and i5 of p = i5 of n. It should regulate the same value for both p and n.
How do i do that? please help me out. I am stuck in this problem from 2 days

Accedi per commentare.

Richiesto:

il 5 Apr 2020

Modificato:

il 28 Mag 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by