How to find elements between zeros in matrix?
Ältere Kommentare anzeigen
Hello, I have this matrix 3x3:
A = [3,4,6,0,2,7,10,5,0,1,0,8,9,0;
0,5,2,0,1,8,9,0,4,6,10,7,0,3;
1,0,7,2,0,4,10,8,0,3,5,0,6,9]
I want to find(specify) elements between zeros for each row, so I can apply some conditions on these elements between zeros. How can I do it with loops and also for large size matrices.
1 Kommentar
Jos (10584)
am 20 Dez. 2017
- Can there be more than 1 zeros between non-zero elements?
- Are there always the same number of blocks of non-zero elements per row?
- What does "apply some conditions" mean?
- What should the output look like? Same size as A? What about the zeros in A?
Akzeptierte Antwort
Weitere Antworten (2)
Matt J
am 20 Dez. 2017
Do not use find(). There is no reason you need actual coordinates for what you describe, and it is just extra computation. Use logical indexing instead:
map=(A~=0);
for i=1:size(A,1)
A(i,map(i,:))=....
end
3 Kommentare
Rik
am 20 Dez. 2017
While it is of course better to use logical indexing whenever you can instead of find, there are some situations in which that is not possible. If you want to fit a curve to specific indices, it may be faster to just use explicit indices, instead of working with a logical matrix.
But yes: in general, use find only if you really can't use logical indexing.
Hajem Daham
am 20 Dez. 2017
Matt J
am 20 Dez. 2017
Hi Matt, it works, but what I want is to reach non-zeros elements without removing zeros, because the final result should contain zeros also.
Shouldn't matter...
Rik
am 20 Dez. 2017
ind=find(A);
[r,c]=find(A);
Kategorien
Mehr zu Creating and Concatenating Matrices 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!