extract the 16 elements (4 by 4 matrix) from a big matrix

2 次查看(过去 30 天)
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
  1 个评论
Stephen23
Stephen23 2019-2-12
You could use a cell array:
Z = mat2cell(A,4,[4,4,4,4]);
Although it is just as easy to access the data directly in the original matrix using basic indexing, without duplicating the data in memory. Splitting up data rarely makes processing data easier.

请先登录,再进行评论。

采纳的回答

KALYAN ACHARJYA
KALYAN ACHARJYA 2019-2-12
编辑:KALYAN ACHARJYA 2019-2-12
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

更多回答(1 个)

Andrei Bobrov
Andrei Bobrov 2019-2-12
n = 4;
s = size(A,1);
out = reshape(A',s,n,[]);
Here:
out(:,:,1) -> B, out(:,:,2) -> C, out(:,:,3) -> D and etc.

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by