MATLAB Answers

How to find position a input

1 ビュー (過去 30 日間)
yanisa ketsuwan
yanisa ketsuwan 2021 年 6 月 19 日
コメント済み: yanisa ketsuwan 2021 年 6 月 20 日
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 件のコメント
yanisa ketsuwan
yanisa ketsuwan 2021 年 6 月 20 日
That’s very kind of you

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by