MATLAB: Extract the 16 elements (4 by 4 matrix) from a big matrix

for loopMATLAB

Now i have a 4 by 16 matrix,we assume this matrix called A
1 2 1 2 5 6 5 6 9 10 9 10 13 14 13 14
3 4 3 4 7 8 7 8 11 12 11 12 15 16 15 16
1 2 1 2 5 6 5 6 9 10 9 10 13 14 13 14
3 4 3 4 7 8 7 8 11 12 11 12 15 16 15 16
i want to extract 4 matrix,16element for each ,that is
1 2 1 2 5 6 5 6 9 10 9 10 13 14 13 14
B= 3 4 3 4 C= 7 8 7 8 D= 11 12 11 12 E= 15 16 15 16
1 2 1 2 5 6 5 6 9 10 9 10 13 14 13 14
3 4 3 4 7 8 7 8 11 12 11 12 15 16 15 16
My thinking is when j=1,n=0,then f = A(1:4 , 1:4) ,so i can get the B matrix
when j=2,n=1,then f = A(1:4 , 5:8) ,so i can get the C matrix
when j=3,n=2,then f = A(1:4 , 9:12) ,so i can get the D matrix
when j=4,n=3,then f = A(1:4 , 13:16) ,so i can get the E matrix
Here is my code,i know this code is not right,but i don't know how to modify it.Can anyone teach me how to modify it to let the code result become what i want ?
for j=1:4
for n=0:3
f=A(1:4 , j+3*n : 4*j);
end
end

Best Answer

Recommended: Comments by S. Cobeldick
As per your specific qiestion
My thinking is when j=1,n=0,then f = A(1:4 , 1:4) ,so i can get the B matrix
when j=2,n=1,then f = A(1:4 , 5:8) ,so i can get the C matrix
when j=3,n=2,then f = A(1:4 , 9:12) ,so i can get the D matrix
when j=4,n=3,then f = A(1:4 , 13:16) ,so i can get the E matrix
you can think about
A=randi(4,16);
n=0;
for j=1:4
f{j}=A(1:4,j+3*n:4*j)
n=n+1;
end
Now you call f{1},f{2}... cell arrays whenever it needed.
3334.png