extract the 16 elements (4 by 4 matrix) from a big matrix
3 views (last 30 days)
Show older comments
yang-En Hsiao
on 12 Feb 2019
Answered: Andrei Bobrov
on 12 Feb 2019
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 Comment
Stephen23
on 12 Feb 2019
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.
Accepted Answer
KALYAN ACHARJYA
on 12 Feb 2019
Edited: KALYAN ACHARJYA
on 12 Feb 2019
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.
0 Comments
More Answers (1)
Andrei Bobrov
on 12 Feb 2019
n = 4;
s = size(A,1);
out = reshape(A',s,n,[]);
Here:
out(:,:,1) -> B, out(:,:,2) -> C, out(:,:,3) -> D and etc.
0 Comments
See Also
Categories
Find more on Resizing and Reshaping Matrices 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!