Find Indexes above zero in a matrix and replace thoses indexes with the value of a vector with the same length.
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Alexandros Samp
am 7 Sep. 2016
Bearbeitet: Andrei Bobrov
am 7 Sep. 2016
Hello, I have matrix A=(112,20) size and a vector with V=(112,1) size. I want to find the indexes in every column in A that are above zero and replace them with the value of the vector with the same index.[x z]=find(A(:,1)>0) returns the indexes and also V(x) returns the vectors with the values i want, but i want to do this for A(:,1),A(:,2)...A(:,20) and store those values in a new Matrix B=(:,20)
2 Kommentare
José-Luis
am 7 Sep. 2016
You want to store the indexes or just get the matrix with the replaced values?
Akzeptierte Antwort
Andrei Bobrov
am 7 Sep. 2016
Bearbeitet: Andrei Bobrov
am 7 Sep. 2016
B = bsxfun(@times,A > 0,V) + A.*(A <= 0);
or
[ii,jj] = find(A > 0);
B = A;
B(sub2ind(size(A),ii,jj)) = V(ii);
2 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!