extract data from matrix and store into a new matirx

I want to extract data of row 6 and the last row from the test matrix. Here is my code:
test=[1 2;13 12; 3 0; 4 0; 5 3; 30 10; 2 10; 30 1;10 4; 50 9; 60 7]
data=zeros(2,2);
for j=1:2
for i=1:6
i=1+5;
data(i,j)=test(i,j)
end
end
data
the correct answer should be
data=[30 10 ; 60 7]
however, it comes up with something wrong.

 Akzeptierte Antwort

Star Strider
Star Strider am 6 Feb. 2018
You do not need the loop.
This works:
data = test([6 end],:);
data =
30 10
60 7

4 Kommentare

Because my actual data is a 145 by 2 matrix matrix.And I want extract those data from row 6 and row6+5 and row row6+5+5.....till the last row of the matrix. In that case, how should I change my code? Or what approach will be better?
Try this:
actual_matrix = randi(9, 145, 2);
data = actual_matrix([6:5:end end], :);
The colon-indexing ends on row 141. If you want the last row as well, the ‘data’ assignment here will work.
If you only want rows 6 through 141 in steps of 5, the assignment becomes simply:
data = actual_matrix(6:5, :);
thank you so much ! it works
As always, my pleasure!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Community Treasure Hunt

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

Start Hunting!

Translated by