not to use loops
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
x=[2,3,4,5;1,3,2,5;8,7,6,5];
y=[-1,2,3,6;8,6,7,5;10,11,12,18];
z=[1,2,3,4;5,18,7,8;12,16,35,-8];
[yuk,idx]=max(z) %as andrei says (http://www.mathworks.com/matlabcentral/answers/17409-without-using-loops)
here z is a function of x and y and the question is to find which corresponds to the max z in each column in the matrices x and y
in short i need to find elements of matrices in each column using idx without using loops For example in the 1. colum max z is 12 and find x is 8 y is 10
3 Kommentare
Daniel Shub
am 4 Okt. 2011
In some ways the loops are perfect. It means you can just borrow a bunch of computers one night and your simulation will be done. Way better than wasting your time trying to optimize code.
Akzeptierte Antwort
Andrei Bobrov
am 4 Okt. 2011
[zout,idx] = max(z);
[m n] = size(x)
ind = sub2ind([m n],idx,1:n)
xout = x(ind)
yout = y(ind)
3 Kommentare
Matt Tearle
am 4 Okt. 2011
I was about to say
[m,n] = size(z);
[~,idx] = max(z);
idx = idx + (0:(n-1))*m
x(idx)
y(idx)
but that's the same as Andrei's. What do you mean by "false in id of maximum numbers"? Compare zout and z(ind) -- they're the same. Try it with random x, y, z -- it's doing what you appeared to ask: find the maximum in each column of z, then return the x and y values from the corresponding locations.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!