hi, I have array a = [5 4 0;3 1 0;5 7 0], how can I replace the value in third column (a(:,3)) with the values in column 1 (a(:,1)) when a(:,1) > a(:,2) . I need obtain [5 3,5] in the third column
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
0 Commenti
Risposta accettata
Image Analyst
il 21 Ott 2016
But when col 1 is less than col 2, you're still replacing it. Just look at your third row. Please make your explanation match your example. You just simply replaced column 3 with column 1 regardless of what column 2 is.
Perhaps you mean this:
m = [5,4,0; 3,1,0; 5,7,0]
% Assign column 1 to column 3
% but only when it's greater than column 2
rowsToReplace = m(:, 1) > m(:, 2)
m(rowsToReplace, 3) = m(rowsToReplace, 1)
Più risposte (1)
Gareth Lee
il 21 Ott 2016
Modificato: Gareth Lee
il 21 Ott 2016
I think the description of your question is not clear, a(:,1)>a(:,2) is a column vector not all true. Is this your judge condition?
if all(a(:,1)>a(:,2))
a(:,3)=a(:,1);
end
Is this that your want ?
3 Commenti
Gareth Lee
il 21 Ott 2016
Since my answer is right, please vote it even though you accept another answer.
Vedere anche
Categorie
Scopri di più su Matrix Indexing in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!