Select range of rows and store them
9 views (last 30 days)
Show older comments
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
Accepted Answer
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}
S{2}
3 Comments
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?
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!