submatrix extraction based on a specific criterion

조회 수: 2 (최근 30일)
Win co
Win co 2014년 5월 11일
댓글: Image Analyst 2014년 5월 12일
Hi everybody, I would like to extract a list of submatrix from an inital matrix, like so:
A=
1 2 3
2 1 2
4 9 1
5 2 4
7 1 0
8 4 8
9 4 1
11 2 4
The 1st column is time in second. I would like to obtain all blocs in which the difference of time between the 1st one and the last one must not be greater than 3s. In this case, we will have:
Block 1:
1 2 3
2 1 2
4 9 1
Block 2:
5 2 4
7 1 0
8 4 8
Block 3:
9 4 1
11 2 4
Could you give me some idea to solve this problem?
Regards,
Winn
  댓글 수: 2
Image Analyst
Image Analyst 2014년 5월 11일
What if it doesn't happen, like if
A=[...
1 2 3
2 1 2
5 2 4
7 1 0
8 4 8
9 4 1
11 2 4];
Would the first block be
1 2 3
2 1 2
5 2 4
or
1 2 3
2 1 2
Win co
Win co 2014년 5월 12일
편집: Win co 2014년 5월 12일
thanks for your very good remark. In fact, time difference must not be above 3s. So in your case, the right one is the second solution. I've edited my post following your remark.

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

채택된 답변

Image Analyst
Image Analyst 2014년 5월 12일
편집: Image Analyst 2014년 5월 12일
How about the brute force approach of a for loop?
clc;
clear 'blocks';
A=[...
1 2 3
2 1 2
4 9 1
5 2 4
7 1 0
8 4 8
9 4 1
11 2 4]
blockStartingRow = 1;
counter = 1;
lastRow = size(A, 1);
for row = 2 : lastRow
if A(row, 1) > A(blockStartingRow, 1) + 3
% Start a block
blocks{counter} = {A(blockStartingRow:(row-1),:)}
counter = counter + 1;
blockStartingRow = row; % Move start of block pointer.
end
end
% Pick up last block if we need to
if blockStartingRow < lastRow
blocks{counter} = {A(blockStartingRow:end,:)}
end
celldisp(blocks)
In the command window:
blocks{1}{1} =
1 2 3
2 1 2
4 9 1
blocks{2}{1} =
5 2 4
7 1 0
8 4 8
blocks{3}{1} =
9 4 1
11 2 4
If you don't know what a cell array is, read the FAQ.
  댓글 수: 3
Win co
Win co 2014년 5월 12일
It works perfectly your code. Thank you very much for your help.
Image Analyst
Image Analyst 2014년 5월 12일
Don't worry about loops. I did one billion , yes billion , loops in just over 2 seconds on my work computer, so a measly million iterations is just a fraction of a second. Even on my slow old home computer I'm clocking a million iterations at 3 milliseconds. If 3 milliseconds is too slow for you, then you'd better lay off the caffeine.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Signal Attributes and Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by