How to reference points in time in a matrix?

3 views (last 30 days)
I have 4 columns of time-series data ranging from t=1 to t=10. I am interested in WHEN each time series become greater than zero. So in this example, the second column would be t=7, for the 3rd column t=5, and for the 4th and 5th columns t=6. I would like to return/generate a row that says t=[7,5,6,6] to represent the point in time for each column when the data becomes greater than 0. I have no clue how to script this and don't even know where to start... Thank you in advance for any suggestions/guidance/tips!
1 0 0 0 0
2 0 0 0 0
3 0 0 0 0
4 0 0 0 0
5 0 0.10 0 0
6 0 0.15 1.82 3.62
7 0.02 0.24 3.58 7.20
8 0.02 0.28 5.13 10.49
9 0.03 0.37 6.63 13.76
10 0.02 0.41 7.95 16.76

Accepted Answer

David Fletcher
David Fletcher on 6 Apr 2021
Edited: David Fletcher on 6 Apr 2021
a=[ 0 0 0 0]
a=[a; 0 0 0 0]
a=[a; 0 0 0 0]
a=[a; 0 0 0 0]
a=[a; 0 0.10 0 0]
a=[a; 0 0.15 1.82 3.62]
a=[a; 0.02 0.24 3.58 7.20]
a=[a; 0.02 0.28 5.13 10.49]
a=[a; 0.03 0.37 6.63 13.76]
a=[a; 0.02 0.41 7.95 16.76]
[r c]=find(a);
loc=[r c];
[loca locb]=ismember(1:size(a,2),loc(:,2));
rows=r(locb)
May need some more extensive testing, but something like this. I suspect it will do something nasty if a column has only zero values.
Alternatively, you may find a loop based solution to be a little less obtuse, and possibly more robust
a=[ 0 0 0 0]
a=[a; 0 0 0 0]
a=[a; 0 0 0 0]
a=[a; 0 0 0 0]
a=[a; 0 0.10 0 0]
a=[a; 0 0.15 1.82 3.62]
a=[a; 0.02 0.24 3.58 7.20]
a=[a; 0.02 0.28 5.13 10.49]
a=[a; 0.03 0.37 6.63 13.76]
a=[a; 0.02 0.41 7.95 16.76]
[rows cols]=size(a); % Find size of matrix
rowLocs=zeros(1,cols); % Set up output vector
for col=1:cols % loop through each column of the matrix
row=1;
while row<=rows % loop through the rows in each column
if a(row,col)~=0 % if a non-zero is found, record row and stop the row loop
rowLocs(col)=row;
break;
end
row=row+1; % next row
end
end
rowLocs % Show locations of first non-zero element in each column
  1 Comment
Wesser
Wesser on 7 Apr 2021
Thanks David! The for/while loop works well!

Sign in to comment.

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!