How to extract and store parts of a matrix to new matrices
8 次查看(过去 30 天)
显示 更早的评论
I have read the FAQ to extract parts of an existing matrix
to new matrices only by selection of rows but had no success.
I wish to do the following:
Let M=
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
and the following parameters to create a loop:
for k=1:3;
l=(k+1);
m=(2*k)+3;
mk=M(l:m,1:end);
end;
I wished by doing that above to get the following results
for k=1
m1=
2 2
3 3
4 4
5 5
for k=2
m2=
3 3
4 4
5 5
6 6
7 7
for k=3
m3=
4 4
5 5
6 6
7 7
8 8
9 9
and so on. Instead, as result I obtained only:
mk=
4 4
5 5
6 6
7 7
8 8
9 9
It is only the matrix for the last value of k and the index 3 is not
appearing instead of k for mk.
Hopefully someone knows how to fix this problem
Thank you
Emerson
0 个评论
采纳的回答
the cyclist
2011-4-2
In your loop, you are writing to the single variable "mk" over and over again, overwriting the value from the previous loop. Probably the best way to do this is with a "cell array". Each element of the cell array can itself be an array, which is what you want.
Use
mk{k}=M(l:m,1:end);
inside the loop, where the "k" inside the curly brackets indexes into the cell array. When you are done, your mk variable will have three elements; each will be an array.
(I did not check the rest of your code to see if it is doing what you want.)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!