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 件のコメント
Ameer Hamza
2020 年 4 月 5 日
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
2020 年 4 月 5 日
回答 (1 件)
Ameer Hamza
2020 年 4 月 5 日
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 件のコメント
Ganesh Kini
2020 年 4 月 5 日
編集済み: Ganesh Kini
2020 年 4 月 5 日
Ameer Hamza
2020 年 4 月 5 日
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
2020 年 4 月 5 日
Ameer Hamza
2020 年 4 月 5 日
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
2020 年 4 月 5 日
編集済み: Ganesh Kini
2020 年 4 月 5 日
Ameer Hamza
2020 年 4 月 5 日
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
2020 年 4 月 5 日
編集済み: Ganesh Kini
2020 年 4 月 5 日
Ameer Hamza
2020 年 4 月 5 日
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
2020 年 4 月 5 日
Ameer Hamza
2020 年 4 月 5 日
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
2020 年 4 月 5 日
Ameer Hamza
2020 年 4 月 5 日
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
2020 年 4 月 6 日
Ameer Hamza
2020 年 4 月 6 日
Ganesh, you will need to share your files and current code so that I can easily suggest a solution.
Ganesh Kini
2020 年 4 月 7 日
Ameer Hamza
2020 年 4 月 7 日
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
2020 年 4 月 7 日
Ameer Hamza
2020 年 4 月 7 日
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
2020 年 4 月 7 日
編集済み: Ganesh Kini
2020 年 4 月 7 日
Ameer Hamza
2020 年 4 月 7 日
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
2020 年 4 月 7 日
Ameer Hamza
2020 年 4 月 7 日
編集済み: Ameer Hamza
2020 年 4 月 7 日
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
2020 年 4 月 16 日
Ameer Hamza
2020 年 4 月 16 日
編集済み: Ameer Hamza
2020 年 4 月 16 日
Use round() function.
Ganesh Kini
2020 年 4 月 16 日
Ameer Hamza
2020 年 4 月 16 日
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
2020 年 4 月 16 日
Ameer Hamza
2020 年 4 月 17 日
Can you attach variable time in a .mat file? It will make it easier to sugges a solution.
Ganesh Kini
2020 年 4 月 17 日
編集済み: Ganesh Kini
2020 年 4 月 17 日
Ameer Hamza
2020 年 4 月 17 日
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
2020 年 4 月 18 日
編集済み: Ganesh Kini
2020 年 4 月 18 日
Ameer Hamza
2020 年 4 月 18 日
Is this the complete error message shown by MATLAB?
Ganesh Kini
2020 年 4 月 18 日
Ameer Hamza
2020 年 4 月 18 日
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.
Ameer Hamza
2020 年 4 月 18 日
I have already posted an answer to that question.
Ganesh Kini
2020 年 4 月 18 日
Ganesh Kini
2020 年 4 月 27 日
編集済み: Ganesh Kini
2020 年 4 月 27 日
Ameer Hamza
2020 年 4 月 27 日
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
2020 年 4 月 27 日
Ameer Hamza
2020 年 4 月 27 日
If 1.1 is uniuq, then a cannot take more then one value. Can you show an example where this happen?
Ganesh Kini
2020 年 4 月 27 日
編集済み: Ameer Hamza
2020 年 4 月 27 日
Ameer Hamza
2020 年 4 月 27 日
How is nw_vec defined?
Ganesh Kini
2020 年 4 月 27 日
編集済み: Ganesh Kini
2020 年 4 月 27 日
Ameer Hamza
2020 年 4 月 27 日
You are just using p5, why are you not using all the indexes: p1,p2,p3,p4,p5,p6,p7?
Ganesh Kini
2020 年 4 月 27 日
編集済み: Ganesh Kini
2020 年 4 月 27 日
Ameer Hamza
2020 年 4 月 27 日
You will need to share period_fun values to diagnose the issue.
Ganesh Kini
2020 年 4 月 27 日
Ameer Hamza
2020 年 4 月 27 日
No value in period_fun is 1.1. How can it find this value?
Ganesh Kini
2020 年 4 月 27 日
編集済み: Ganesh Kini
2020 年 4 月 27 日
Ameer Hamza
2020 年 4 月 27 日
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
2020 年 4 月 27 日
Ameer Hamza
2020 年 4 月 27 日
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
2020 年 4 月 27 日
Ameer Hamza
2020 年 4 月 27 日
are you running it in MATLAB?
Ganesh Kini
2020 年 4 月 27 日
編集済み: Ganesh Kini
2020 年 4 月 27 日
Ameer Hamza
2020 年 4 月 27 日
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
2020 年 5 月 28 日
編集済み: Ganesh Kini
2020 年 5 月 28 日
カテゴリ
ヘルプ センター および File Exchange で Special Characters についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!