Say that I have the vectors
v1=[1 2 3]
v2=[4 3 8]
how can I, by using these two, create the following vector
v3=[4 3 3 8 8 8]
i.e., a vector with one 4s, two 3s and three 8s.

 Akzeptierte Antwort

David Hill
David Hill am 4 Apr. 2022

1 Stimme

v=repelem(v2,v1);

3 Kommentare

Thanks, a follow-up question, I have this matrix
X=[9 6 9; 0 2 nan; 3 1 2];
How can I easly find the number of elements of each row? I.e., get the vector
v=[3;2;3]
David Hill
David Hill am 4 Apr. 2022
sum(~isnan(X),2)
Sebastian Daneli
Sebastian Daneli am 4 Apr. 2022
Great, thanks.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by