필터 지우기
필터 지우기

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 CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by