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
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 个)

Community Treasure Hunt

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

Start Hunting!

Translated by