combining lots of vectors of unequal length

1 visualizzazione (ultimi 30 giorni)
Haneya Qureshi
Haneya Qureshi il 3 Apr 2018
Commentato: Stephen23 il 3 Apr 2018
i have: a=[1 2 3 0 0] b=[4 5 6 7 0 0 0] c=[8 9] i want: d= [a b c] with zeros removed d=[1 2 3 4 5 6 7 8 9]
Is there a generic code for this? because i have lots of vectors like a,b and c and their length is very large and unequal

Risposte (1)

Akira Agata
Akira Agata il 3 Apr 2018
I don't think there is a generic code. How about making a function to do this, like:
function [d1,d2] = yourFunction(a,b,c)
d1 = [a,b,c];
idx = d1 == 0;
d2 = d1(~idx);
end
Here is an example.
>> [d1,d2] = yourFunction([1 2 3 0 0],[4 5 6 7 0 0 0],[8 9])
d1 =
1 2 3 0 0 4 5 6 7 0 0 0 8 9
d2 =
1 2 3 4 5 6 7 8 9

Categorie

Scopri di più su Resizing and Reshaping Matrices 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