Effacer les filtres
Effacer les filtres

How to split a matrix

1 vue (au cours des 30 derniers jours)
Amy Xu
Amy Xu le 9 Avr 2017
Modifié(e) : the cyclist le 10 Avr 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
];

Réponses (1)

the cyclist
the cyclist le 10 Avr 2017
Modifié(e) : the cyclist le 10 Avr 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.

Catégories

En savoir plus sur Multidimensional Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by