Filter löschen
Filter löschen

How to split a matrix

1 Ansicht (letzte 30 Tage)
Amy Xu
Amy Xu am 9 Apr. 2017
Bearbeitet: the cyclist am 10 Apr. 2017
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
];

Antworten (1)

the cyclist
the cyclist am 10 Apr. 2017
Bearbeitet: the cyclist am 10 Apr. 2017
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.

Kategorien

Mehr zu Multidimensional Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by