# combine vectors in different length in to one matrix

94 vues (au cours des 30 derniers jours)
yuh zhang le 6 Avr 2022
Commenté : yuh zhang le 8 Avr 2022
hello, I want to combine 100 row vectors in different lengths in to one big matrix, say:
v1 = [1 2 3]; v2 = [1 2 3 4]; v3 = [1]; ....; vn = [xx xx xx ... xx];
A = [1 2 3 0 0; 1 2 3 4 0; 1 0 0 0 0; xx xx xx xx xx];
I want to find the max length in these row vectors, say 5, then if the length of the other vector is smaller than 5, then we set 0 in the rest position, how can I do that?
Thanks!
##### 1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
yuh zhang le 8 Avr 2022

Connectez-vous pour commenter.

### Réponse acceptée

Simon Allosserie le 6 Avr 2022
You can combine vectors of different lengths in cell arrays, instead of adding zeros to your vectors. It will be easier to process them also, because then they are together in one structure that you can loop through (instead of having to acces each vector v1, v2, ... separately). See https://nl.mathworks.com/help/matlab/ref/cell.html
If you however per se want to put them together with a lot of zeros, I'd suggest something like this:
First find the maximum vector length, let's call it maxLength;
Then define a matrix A sized (n, maxLength) with n the number of vectors you have
A = zeros(n, maxLength);
A(i,1:lengthVi) = vi;
Hope this helps.
##### 1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
Stephen23 le 6 Avr 2022

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Matrix Indexing 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