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!