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일

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

yanisa ketsuwan
yanisa ketsuwan 2021년 6월 19일
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.
Jan
Jan 2021년 6월 19일
Yes, this is correct: The minimal distance it found at x(i1), y(i2), xx(i3), yy(i4).
yanisa ketsuwan
yanisa ketsuwan 2021년 6월 20일
That’s very kind of you

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

질문:

2021년 6월 19일

댓글:

2021년 6월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by