MATLAB: Finding the longest row in an array from CSVREAD

csvrreadlength of array

Hey guys,
I've read in a text file from csvread. The text file was something like this:
1, 1, 1, 1, 1
2, 2, 2, 2, 2, 2, 2, 2, 2, 2
3, 3, 3, 3, 3, 3, 3
After reading it in with csvread, I get the array:
1, 1, 1, 1, 1, 0, 0, 0, 0, 0
2, 2, 2, 2, 2, 2, 2, 2, 2, 2
3, 3, 3, 3, 3, 3, 3, 0, 0, 0
The only problem with this is that I want to be able to determine the length of each row excluding the zeros that have automatically been appended in order to keep the matrix square.
The significance of my problem is that these number represent a stress signal and I need to count the cycles in each signal. There are only 5 cycles in the first row, but simply taking
[cycles, ~] = size(matrix);
Will return 10 cycles because it includes the zeros after the 1s. This is bad because when my code performs a damage calculation on the signal it will count 10 cycles every time when it should only could 5, 10 and 7 cycles.
Is there a way of being able to determine the length of each signal so that I get the correct values i.e. 5, 10 and 7 for the aforementioned text file?
Thanks!

Best Answer

If your data is stored in array D and is non-zero except for the added elements, one way to obtain the length of each row is the following:
l = size(D,2) - sum(D==0, 2) ;
where l is a column vector of lengths.