submatrix extraction based on a specific criterion

1 Ansicht (letzte 30 Tage)
Win co
Win co am 11 Mai 2014
Kommentiert: Image Analyst am 12 Mai 2014
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 Kommentare
Image Analyst
Image Analyst am 11 Mai 2014
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 am 12 Mai 2014
Bearbeitet: Win co am 12 Mai 2014
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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Image Analyst
Image Analyst am 12 Mai 2014
Bearbeitet: Image Analyst am 12 Mai 2014
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 Kommentare
Win co
Win co am 12 Mai 2014
It works perfectly your code. Thank you very much for your help.
Image Analyst
Image Analyst am 12 Mai 2014
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by