How to find position a input
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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.
0 Kommentare
Antworten (1)
Jan
am 19 Jun. 2021
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 Kommentare
Jan
am 19 Jun. 2021
Yes, this is correct: The minimal distance it found at x(i1), y(i2), xx(i3), yy(i4).
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!