フィルターのクリア

How to split a matrix

1 回表示 (過去 30 日間)
Amy Xu
Amy Xu 2017 年 4 月 9 日
編集済み: the cyclist 2017 年 4 月 10 日
Matrix G is as follows:
G = [
1 2 3 1 0
1 2 1 0 0
1 4 3 1 2
1 3 4 2 0
1 0 0 0 0
];
I want to produce 5 set of matrix. The first set include only first column. The second set include the second and first columns from matrix G. The 3rd set includes the third, second and first columns from matrix G. ...
OUT1 = [
1
1
1
1
1
];
OUT2 = [
2 1
2 1
4 1
3 1
];
OUT3 = [
3 2 1
1 2 1
3 4 1
4 3 1
];
OUT4 = [
1 3 2 1
1 3 4 1
2 4 3 1
];
OUT5 = [
2 1 3 4 1
];

回答 (1 件)

the cyclist
the cyclist 2017 年 4 月 10 日
編集済み: the cyclist 2017 年 4 月 10 日
G = [
1 2 3 1 0
1 2 1 0 0
1 4 3 1 2
1 3 4 2 0
1 0 0 0 0
];
ncols = size(G,2);
OUT = cell(ncols,1);
for nc = 1:ncols
OUT{nc} = fliplr(G(:,1:nc));
OUT{nc}(any(OUT{nc}==0,2),:) = [];
end
Note that I used a cell array, rather than variables names with embedded numbers, which are bad coding practice.

カテゴリ

Help Center および File ExchangeMultidimensional Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by