How to reference points in time in a matrix?

조회 수: 1 (최근 30일)
Wesser
Wesser 2021년 4월 6일
댓글: Wesser 2021년 4월 7일
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

채택된 답변

David Fletcher
David Fletcher 2021년 4월 6일
편집: David Fletcher 2021년 4월 6일
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
Wesser
Wesser 2021년 4월 7일
Thanks David! The for/while loop works well!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by