Replacing elements of a Matrix meeting conditional
조회 수: 8 (최근 30일)
이전 댓글 표시
Hi, I have two matrixes and wish to replace the first column of matrix A, with elements of the fourth column of matrix B, but just in the case the elements of the first columns of A and B are equal.
I tried the following, but its probably not correct. Can someone help me out?
for j=1:size(A,1)
if A(j,1)==B(:,1);
A(j,1)=B(:,4);
end
end
댓글 수: 0
채택된 답변
Fangjun Jiang
2011년 8월 6일
A=rand(14,5);A(:,1)=1:14;
B=rand(5,4);B(:,1)=10:-2:2;B(:,4)=10*B(:,1);
[Dummy,IndexA,IndexB]=intersect(A(:,1),B(:,1));
A(IndexA,1)=B(IndexB,4)
추가 답변 (2개)
Paulo Silva
2011년 8월 6일
%sample arrays
a=randi([1 6],4,4);
b=randi([1 6],4,4);
e=a(:,1)==b(:,1); %find what elements are equal in first columns
a(e,1)=b(e,4) %replace the elements
참고 항목
카테고리
Help Center 및 File Exchange에서 Cell Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!