How to find the value closest to 1 from a x*y*z double matrix ?

1 view (last 30 days)
I have to find the values for k according to the following code. When I execute the following code I get a 18x12x6 double to k. From that how can I find the exact p,n and m which is corresponding a value which is closest to 1 from matrix k??
I1= 8.0742;
I2=4.85;
I3=2.4293;
for p=1:18
for n=1:12
for m=1:6
k(p,n,m) = p*I1/(n*I2+m*I3);
end
end
end

Accepted Answer

Stephen23
Stephen23 on 28 Oct 2018
Edited: Stephen23 on 28 Oct 2018
>> [~,x] = min(abs(k(:)-1));
>> [p,n,m] = ind2sub(size(k),x)
p = 6
n = 9
m = 2
>> k(p,n,m)
ans = 0.99869
  5 Comments
Stephen23
Stephen23 on 30 Oct 2018
Use sort instead of min, and pick as many as you want:
>> [v,x] = sort(abs(k(:)-1));
>> [p,n,m] = ind2sub(size(k),x(1:3)) % first three
p =
6
3
6
n =
9
4
8
m =
2
2
4
>> k(p(1),n(1),m(1))
ans = 0.99869
>> k(p(2),n(2),m(2))
ans = 0.99852
>> k(p(3),n(3),m(3))
ans = 0.99852

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!