フィルターのクリア

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

2 ビュー (過去 30 日間)
yang-En Hsiao
yang-En Hsiao 2019 年 2 月 12 日
回答済み: Andrei Bobrov 2019 年 2 月 12 日
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 Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by