# Extract the value of a array from a function

4 visualizzazioni (ultimi 30 giorni)
Ganesh Kini il 5 Apr 2020
Modificato: Ganesh Kini il 28 Mag 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
##### 2 CommentiMostra NessunoNascondi Nessuno
Ameer Hamza il 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 il 5 Apr 2020
Yes, it will be only integers.
how do i write the inverse of it ?
Please let me know

Accedi per commentare.

### Risposte (1)

Ameer Hamza il 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 CommentiMostra 55 commenti meno recentiNascondi 55 commenti meno recenti
Ameer Hamza il 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 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.

### Categorie

Scopri di più su Variables in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by