Extract sub matrices
8 views (last 30 days)
Show older comments
I have a matrix A=magic(4); (say)
I want 4x4 matrices from A such that the 16 matrices can be saved with different names.
a1=(16 2;5 11)
a2=(16 2 3; 5 11 10)
a3=( 2 3 13; 11 10 8)
a4=(3 13; 3 13)
a5=(16 2; 5 11; 9 7)
a6=(16 2 3; 5 11 10; 9 7 6 )
a7=(2 3 13; 11 10 8; 7 6 12)
a8=(3 13; 10 8;6 12)
a9=(5 11; 9 7;4 14)
a10= (5 11 10 ; 9 7 6; 4 14 15)
a11=(11 10 8; 7 6 12; 14 15 1)
a12=(10 8; 6 12; 15 1)
a13=(9 7; 4 14 )
a14=(9 7 6; 4 14 15)
a15=(7 6 12; 14 15 1)
a16=( 6 12; 15 1)
I want to use loop so that I can extract matrices from other large matrices.
0 Comments
Answers (2)
Image Analyst
on 15 Mar 2012
That's okay to hard code for a small matrix, like you did (almost), but not advisable for a large matrix. See the FAQ http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F
0 Comments
Jan
on 15 Mar 2012
I use A1, A2, ... for arrays up to 3 elements only. Otherwise using a cell A{1}, A{2}, ... is much more convenient, efficient and easier to debug. Reading the FAQ is recommended. At least the best-of, because it is asked with such a high frequency, that it gets a waste of time:
- FAQ 4.6: Create A1, A2, ... in a loop
- FAQ 6.1: 0.3 - 0.2 - 0.1 ~= 0
- FAQ 4.16: Avoid eval
- FAQ 5.16: Share data between callbacks
This should not encourage you not to read the other sections also.
0 Comments
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!