Extract sub matrices

조회 수: 9 (최근 30일)
Abhijit Das
Abhijit Das 2012년 3월 15일
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.

답변 (2개)

Image Analyst
Image Analyst 2012년 3월 15일
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

Jan
Jan 2012년 3월 15일
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:
  1. FAQ 4.6: Create A1, A2, ... in a loop
  2. FAQ 6.1: 0.3 - 0.2 - 0.1 ~= 0
  3. FAQ 4.16: Avoid eval
  4. FAQ 5.16: Share data between callbacks
This should not encourage you not to read the other sections also.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by