submatrix extraction based on a specific criterion
조회 수: 2 (최근 30일)
이전 댓글 표시
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
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
채택된 답변
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
댓글 수: 3
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개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!