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

Daniel Shub
Daniel Shub 2011년 10월 4일
Over the past 10 years or so the JIT accelerator has substantially sped up loops in MATLAB. While there are lots of cases where vectorized code is still faster, if you can solve your problems with a loop, there may be no reason to try and do it without a loop (i.e., the improvement in performance may not be worth it).
osman
osman 2011년 10월 4일
but my advisor didnt accept this solution with 2 loops. our simulation take about 2 day and we need to find a way.
Daniel Shub
Daniel Shub 2011년 10월 4일
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.

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

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2011년 10월 4일

0 개 추천

[zout,idx] = max(z);
[m n] = size(x)
ind = sub2ind([m n],idx,1:n)
xout = x(ind)
yout = y(ind)

댓글 수: 3

osman
osman 2011년 10월 4일
it doesn't respond correct every time. false in id of maximum numbers.
maximum numbers id can be randomly disturbuted
Matt Tearle
Matt Tearle 2011년 10월 4일
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.
osman
osman 2011년 10월 5일
ok i misunderstood the output. it fits the problem. i gonna accept the answer

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

질문:

2011년 10월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by