MATLAB Answers

Extract the value of a array from a function

6 views (last 30 days)
Ganesh Kini
Ganesh Kini on 5 Apr 2020
Edited: Ganesh Kini on 28 May 2020
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


Ameer Hamza
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
Ganesh Kini on 5 Apr 2020
Yes, it will be only integers.
how do i write the inverse of it ?
Please let me know

Sign in to comment.

Answers (1)

Ameer Hamza
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);


Ganesh Kini
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);
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
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
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

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!