How to find position a input

Hello!
I have this function
function z = testpapa(x, y,xx,yy)
z = zeros(2,2,2,2);
for m=1:length(x)
for n=1:length(y)
for i=1:length(xx)
for ii=1:length(yy)
z(m,n,i,ii) = (x(m)).^4'* sin(y(n)).^3'*cos(xx(i)).^2'*(yy(ii));
end
end
end
end
end
When implementing a function, I picked one value, but what I want is the input position of each one that brings out this value. how to find position a input (x y xx yy).
x = 5:1:6; y = 7:1:8; xx = 9:1:10; yy = 11:1:12;
fcn = testpapa(x,y,xx,yy);
V =0.1264; % required value
dif = abs(fcn-V);
minMatrix = min(dif(:));
[row;col;roll;coll] = find(dif==minMatrix);
Thanks for everyone's answers.

Answers (1)

z(m,n,i,ii) = (x(m)).^4'* sin(y(n)).^3'*cos(xx(i)).^2'*(yy(ii));
% Voodoo: ^ ^^ ^ ^ ^ ^ ^ ^ ^
% Cleaner:
z(m,n,i,ii) = x(m)^4 * sin(y(n))^3 * cos(xx(i))^2 * yy(ii);
You could omit the loops also:
z = x(:).^4 .* sin(y(:).').^3 .* ...
reshape(cos(xx).^2, 1, 1, []) .* reshape(yy, 1, 1, 1, []);
I'm not sure if this is nicer.
V = 0.1264; % required value
dif = abs(fcn - V);
[minDif, iLinear] = min(dif(:));
[i1, i2, i3, i4] = ind2sub(size(dif), find(dif == minDif))

3 Comments

You’re so helpful. Thanks a lot.
Can I ask you something? I want the position xx at V So wrote " xx(i3) ". I'm not sure if I understand it correctly.
Yes, this is correct: The minimal distance it found at x(i1), y(i2), xx(i3), yy(i4).
That’s very kind of you

Sign in to comment.

Categories

Asked:

on 19 Jun 2021

Commented:

on 20 Jun 2021

Community Treasure Hunt

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

Start Hunting!