extract the 16 elements (4 by 4 matrix) from a big matrix
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
yang-En Hsiao
il 12 Feb 2019
Risposto: Andrei Bobrov
il 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 Commento
Stephen23
il 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.
Risposta accettata
KALYAN ACHARJYA
il 12 Feb 2019
Modificato: KALYAN ACHARJYA
il 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.
![3334.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/203771/3334.png)
0 Commenti
Più risposte (1)
Andrei Bobrov
il 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 Commenti
Vedere anche
Categorie
Scopri di più su Resizing and Reshaping Matrices in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!