Can you index matrices?

Suppose I have a (n x p) matrix M. Can a "for" loop be written to partition M into a set of smaller matrices A_i where A_1 = the first r_1 rows of M, A_2 = the next r_2 rows of M, A_3 = the next r_3 rows of M,........., A_k = the last r_k rows of M where k<=n ?
I know I can index into M easily with A(i,j) being the element corresponding to the i'th row and j'th column of M. What I want to do is "pull out'' matrices from M: A_1, A_2, A_3, .... , A_k as described above. I don't want to define each matrix manually however as I would have to define thousands of sub matrices A_i !
Any help would be greatly appreciated.
Jonathan

2 件のコメント

Image Analyst
Image Analyst 2013 年 1 月 28 日
These sentences seem contradictory: "What I want to do is "pull out'' matrices from M: A_1, A_2, A_3, .... , A_k as described above. I don't want to define each matrix manually however" So do you want separate matrices (A_1, A_2, etc.), or separate cells in a cell array (A{1}, A{2}, etc.) or not? I can tell from what you said. You said you do but in the next sentence you said you don't.
Cedric
Cedric 2013 年 1 月 28 日
The way I understood it is that he doesn't want to write manually expressions like:
A_1 = [M(1,1),M(1,2),M(1,3); M(2,1),M(2,2),M(2,3)] ;
...

サインインしてコメントする。

 採用された回答

Cedric
Cedric 2013 年 1 月 28 日
編集済み: Cedric 2013 年 1 月 28 日

0 投票

The following:
M(5:8, 3:7)
is an array that corresponds to the block defined by rows 5 to 8 of M and columns 3 to 7. As these ranges can be defined by vectors/arrays content, you can build flexible solutions for block-indexing M.
rIds = ... ; % rIds and cIds could be defined as a function of a loop
cIds = ... ; % index variable, e.g. based on vectors of block
% boundaries in terms of row IDs and column IDs:
% rIds = rBoundaries(ii):rBoundaries(ii+1) ; ..
block = M(rIds, cIds) ;
If you wanted your blocks to be stored separately in a cell arrays, you could use the function mat2cell(). It would take M as a first arg and essentially the two vectors of block boundaries mentioned above as second/third args.

1 件のコメント

Jonathan Ross
Jonathan Ross 2013 年 1 月 31 日
Thank you Cedric. Just what I needed. I wasn't aware of the mat2cell function in Matlab.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by