How do i create a loop that accesses more than one matrix?

조회 수: 1 (최근 30일)
Kirsten du plessis
Kirsten du plessis 2015년 2월 17일
댓글: Kirsten du plessis 2015년 2월 17일
I'm relatively new to MATLAB, but I'm trying to create a loop that runs through one column in matrix X, matches the value to another column in matrix Y, and then feeds the corrosponding values in the second and third columns of matrix Y into the second and third columns of matrix X?
I've been trying to use two for loops, however i cant get it to refence the second and thtird columns. How would i go about doing this?
Thanks!!

채택된 답변

Yoav Livneh
Yoav Livneh 2015년 2월 17일
편집: Yoav Livneh 2015년 2월 17일
Maybe try something like this
for jj = 1:size(X,1)
% match 1st column of X to 1st column in Y
idx = find(X(jj,1) == Y(:,1));
% put 2nd and 3rd columns in Y into X
X(idx,2) = Y(idx,2);
X(idx,3) = Y(idx,3);
end
Hope this is what you meant.
  댓글 수: 3
Yoav Livneh
Yoav Livneh 2015년 2월 17일
Hmm the code seems to work fine for me. Can you print your X and Y matrices here?
By the way, I made a mistake, you should switch the last two lines to:
X(jj,2) = Y(idx,2);
X(jj,3) = Y(idx,3);
Kirsten du plessis
Kirsten du plessis 2015년 2월 17일
Yes thanks - i worked that out eventually :) Thanks for your help!!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by