combining lots of vectors of unequal length

조회 수: 1 (최근 30일)
Haneya Qureshi
Haneya Qureshi 2018년 4월 3일
댓글: Stephen23 2018년 4월 3일
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

답변 (1개)

Akira Agata
Akira Agata 2018년 4월 3일
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

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by