You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Extract the value of a array from a function
5 views (last 30 days)
Show older comments
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 Comments
Ameer Hamza
on 5 Apr 2020
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?
Ganesh Kini
on 5 Apr 2020
Yes, it will be only integers.
how do i write the inverse of it ?
Please let me know
Answers (1)
Ameer Hamza
on 5 Apr 2020
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 Comments
Ganesh Kini
on 5 Apr 2020
Edited: Ganesh Kini
on 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
Ameer Hamza
on 5 Apr 2020
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.
Ganesh Kini
on 5 Apr 2020
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
Ameer Hamza
on 5 Apr 2020
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
on 5 Apr 2020
Edited: Ganesh Kini
on 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 ?
Ameer Hamza
on 5 Apr 2020
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
on 5 Apr 2020
Edited: Ganesh Kini
on 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
Ameer Hamza
on 5 Apr 2020
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.
Ganesh Kini
on 5 Apr 2020
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
Ameer Hamza
on 5 Apr 2020
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.
Ganesh Kini
on 5 Apr 2020
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
Ameer Hamza
on 5 Apr 2020
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?
Ganesh Kini
on 6 Apr 2020
Hi,
Could you please help me with other source. I tried it is not working
Ameer Hamza
on 6 Apr 2020
Ganesh, you will need to share your files and current code so that I can easily suggest a solution.
Ganesh Kini
on 7 Apr 2020
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 ?
Ameer Hamza
on 7 Apr 2020
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);
Ganesh Kini
on 7 Apr 2020
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
Ameer Hamza
on 7 Apr 2020
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
on 7 Apr 2020
Edited: Ganesh Kini
on 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 ?
Ameer Hamza
on 7 Apr 2020
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
Ganesh Kini
on 7 Apr 2020
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 ?
Ameer Hamza
on 7 Apr 2020
Edited: Ameer Hamza
on 7 Apr 2020
You can get the value of a,b,...h back using
[a,b,c,d,e,f,g,h] = ind2sub(size(period_fun), 62988);
Ganesh Kini
on 16 Apr 2020
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
Ganesh Kini
on 16 Apr 2020
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 ?
Ameer Hamza
on 16 Apr 2020
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
Ganesh Kini
on 16 Apr 2020
Hi
The above code is not working
I have tried this before
Can you suggest some other alternative ?
Ameer Hamza
on 17 Apr 2020
Can you attach variable time in a .mat file? It will make it easier to sugges a solution.
Ganesh Kini
on 17 Apr 2020
Edited: Ganesh Kini
on 17 Apr 2020
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
Ameer Hamza
on 17 Apr 2020
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);
Ganesh Kini
on 18 Apr 2020
Edited: Ganesh Kini
on 18 Apr 2020
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
Ameer Hamza
on 18 Apr 2020
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.
Ganesh Kini
on 27 Apr 2020
Edited: Ganesh Kini
on 27 Apr 2020
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,)
Ameer Hamza
on 27 Apr 2020
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);
Ganesh Kini
on 27 Apr 2020
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 ?
Ameer Hamza
on 27 Apr 2020
If 1.1 is uniuq, then a cannot take more then one value. Can you show an example where this happen?
Ganesh Kini
on 27 Apr 2020
Edited: Ameer Hamza
on 27 Apr 2020
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 ?
Ganesh Kini
on 27 Apr 2020
Edited: Ganesh Kini
on 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
Ameer Hamza
on 27 Apr 2020
You are just using p5, why are you not using all the indexes: p1,p2,p3,p4,p5,p6,p7?
Ganesh Kini
on 27 Apr 2020
Edited: Ganesh Kini
on 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
Ganesh Kini
on 27 Apr 2020
Yes I have shared it earlier on this link
its the same code
Attaching the file again
Ganesh Kini
on 27 Apr 2020
Edited: Ganesh Kini
on 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
Ameer Hamza
on 27 Apr 2020
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
Ganesh Kini
on 27 Apr 2020
period_arr(2,1,1,9,9,13,8);
can you try this combination ??
Just to cross verify
Ameer Hamza
on 27 Apr 2020
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
Ganesh Kini
on 27 Apr 2020
Edited: Ganesh Kini
on 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
Ameer Hamza
on 27 Apr 2020
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
on 28 May 2020
Edited: Ganesh Kini
on 28 May 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
See Also
Categories
Find more on Elementary Math in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)