Select range of rows and store them

9 views (last 30 days)
Enzo
Enzo on 27 Oct 2022
Commented: Enzo on 28 Oct 2022
Hello everyone,
that's the issue:
I have a matrix, let's call it Ch = 1000 x 33. In column 33, I have spotted some values using a threshold (MinPeakHeight function), which returns the location (lc) of these rows (let's call them rows_X). Now, using these indices, I would like to retain all the values within 3 rows before rows_X and 4 rows after every rows_X (the value inside rows_X must be retained as well). Any help will be very appreciated!!
Ex: let's take the following matrix. I have spotted all the values in column 6 greater than 7.Now I know these values lies in rows 3 and 8. I want to retain/store in different variables all the values in rows 3 and 8, plus all the values in the 2 rows before rows 3 and 8, and in 2 rows after rows 3 and 8.
Ch:
1 4 5 4 8 4
2 5 5 4 9 4
3 5 5 5 5 8
4 6 5 6 5 4
5 6 5 6 5 4
6 6 5 6 5 5
7 7 5 6 5 5
8 6 7 6 5 8
9 3 3 3 3 3
10 3 3 4 4 3
the final output should look like this:
rows_a =
1 4 5 4 8 4
2 5 5 4 9 4
3 5 5 5 5 8
4 6 5 6 5 4
5 6 5 6 5 4
rows_b =
6 6 5 6 5 5
7 7 5 6 5 5
8 6 7 6 5 8
9 3 3 3 3 3
10 3 3 4 4 3
  3 Comments
enzo
enzo on 27 Oct 2022
@Matt J the first one was the actual scenario. The sample, where I asked minus 2 ans plus 2 rows from row_x is just for demonstrational purpose and in order to be easier to be shown to the matlab community
Enzo
Enzo on 27 Oct 2022
@Matt J do you have any tips? please, fell free to share any doubt about this task

Sign in to comment.

Accepted Answer

David Hill
David Hill on 27 Oct 2022
Edited: David Hill on 27 Oct 2022
Use of cell array for storage incase the sizes are not equal. If the sizes will always be the same, you could store in 3D-matrix.
Ch=[1 4 5 4 8 4
2 5 5 4 9 4
3 5 5 5 5 8
4 6 5 6 5 4
5 6 5 6 5 4
6 6 5 6 5 5
7 7 5 6 5 5
8 6 7 6 5 8
9 3 3 3 3 3
10 3 3 4 4 3];
idx=find(Ch(:,6)>7);
for k=1:length(idx)
S{k}=Ch(max(1,idx(k)-2):min(size(Ch,1),idx(k)+2),:);%max and min to protect against to low or high of indexing
end
S{1}
ans = 5×6
1 4 5 4 8 4 2 5 5 4 9 4 3 5 5 5 5 8 4 6 5 6 5 4 5 6 5 6 5 4
S{2}
ans = 5×6
6 6 5 6 5 5 7 7 5 6 5 5 8 6 7 6 5 8 9 3 3 3 3 3 10 3 3 4 4 3
  3 Comments
David Hill
David Hill on 27 Oct 2022
It matches the output above other than assigning different variables (should index into a single variable and not have dynamic variable names). What are you trying to do once you have the various matrices?
Enzo
Enzo on 28 Oct 2022
hi @David Hill thanks for your time. I have tried again and, as I am using huge data files, matlab send me this error code
Out of memory.
reasonably, I have to keep it simple without killing the ram

Sign in to comment.

More Answers (0)

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!