Filter löschen
Filter löschen

combining lots of vectors of unequal length

1 Ansicht (letzte 30 Tage)
Haneya Qureshi
Haneya Qureshi am 3 Apr. 2018
Kommentiert: Stephen23 am 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

Antworten (1)

Akira Agata
Akira Agata am 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

Kategorien

Mehr zu Matrices and 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