How to find position a input

2 次查看(过去 30 天)
yanisa ketsuwan
yanisa ketsuwan 2021-6-19
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.

回答(1 个)

Jan
Jan 2021-6-19
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 个评论
Jan
Jan 2021-6-19
Yes, this is correct: The minimal distance it found at x(i1), y(i2), xx(i3), yy(i4).

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by