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 个评论
采纳的回答
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}
S{2}
3 个评论
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?
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!