change one column of matrix
14 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
NA
il 28 Lug 2019
Commentato: Walter Roberson
il 28 Lug 2019
I have A
A=[1,0,4,0,5,0;0,1,0,3,-1,0;0,4,0,0,0,8];
B=[1.2,-1,-2.1,-1.3,-1.4,1;-2.1,1.3,-1,-2,-3,-1.8;1.9,-1.2,3.1,2.7,2.5,-0.5];
index=2;
I want to add nonzero element in A to corresponding B element.
A(index,:)=B(index,A(index, :)~=0)+A(index, :);
result should be
A=[1,0,4,0,5,0; 0, 1+1.3,0,3-2,-1-3,0; 0,4,0,0,0,8];
0 Commenti
Risposta accettata
Walter Roberson
il 28 Lug 2019
mask = A(index, :) ~= 0;
A(index, mask) = A(index, mask) + B(index, mask) ;
3 Commenti
Più risposte (1)
Vedere anche
Categorie
Scopri di più su Structures in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!