Select range of rows and store them

4 ビュー (過去 30 日間)
Enzo
Enzo 2022 年 10 月 27 日
コメント済み: Enzo 2022 年 10 月 28 日
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 Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by