i have a matirx: new_mat= [ 0 0 0 0 1 0 0 0 0; 0 0 0 2 6 10 0 0 0 ; 0 0 3 7 11 15 19 0 0; 0 4 8 12 16 20 24 28 0; 0 14 18 22 26 0 0 0 0 ; 0 0 0 0 0 30 34 38 0] i want the first and last non zero number of each row in the matrix as first= 1; 2; 3; 4;14; 30] and last=[1; 10; 19; 28; 26; 38]; how do i get these values? I can use loops or even the find function.
Asked
Active
Viewed 185 times
0
-
Possible duplicate of [Matlab: First Non-zero element of each row or column](https://stackoverflow.com/questions/19624794/matlab-first-non-zero-element-of-each-row-or-column) – beaker Aug 07 '17 at 18:55
-
i did try it, but that code gives the index i just need the numbers that's the first non-zero! – Sakshi ajit sinha Aug 09 '17 at 09:02
-
Then I would suggest [this solution](https://stackoverflow.com/a/19624967/1377097) and rather than returning the results of `find(...)`, return the results of `row(find(...))`. Also note the comment there: if there is a possibility that your rows might have all zeros, you'll need to set `UniformOutput` to `false`. – beaker Aug 09 '17 at 14:34