Azzera filtri
Azzera filtri

How to split a matrix

1 visualizzazione (ultimi 30 giorni)
Amy Xu
Amy Xu il 9 Apr 2017
Modificato: the cyclist il 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
];

Risposte (1)

the cyclist
the cyclist il 10 Apr 2017
Modificato: the cyclist il 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.

Categorie

Scopri di più su Multidimensional Arrays 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!

Translated by