Select range of rows and store them

9 次查看(过去 30 天)
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 个评论
enzo
enzo 2022-10-27
@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 2022-10-27
@Matt J do you have any tips? please, fell free to share any doubt about this task

请先登录,再进行评论。

采纳的回答

David Hill
David Hill 2022-10-27
编辑:David Hill 2022-10-27
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 个评论
David Hill
David Hill 2022-10-27
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 2022-10-28
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by