Replacing elements of a Matrix meeting conditional
Ältere Kommentare anzeigen
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
Akzeptierte Antwort
Weitere Antworten (2)
Paulo Silva
am 6 Aug. 2011
%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
1 Kommentar
Patricia
am 7 Aug. 2011
Patricia
am 7 Aug. 2011
0 Stimmen
1 Kommentar
Fangjun Jiang
am 7 Aug. 2011
Nice! I also updated my answer. It's similar, for your reference.
Kategorien
Mehr zu Variables finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!