how to do repeating numbers in array

1 Ansicht (letzte 30 Tage)
Murugan C
Murugan C am 19 Sep. 2018
Kommentiert: Murugan C am 20 Sep. 2018
Hi
I have an array like [1 0 0 0 2 0 0 0 3 0 0 0 0 0 5 0 0 0 0].
I want output like, [1 1 1 1 2 2 2 2 3 3 3 3 3 3 5 5 5 5 5].
how i can do this?
Thanks in Advance!!!
Murugan

Akzeptierte Antwort

Stephen23
Stephen23 am 19 Sep. 2018
Bearbeitet: Stephen23 am 19 Sep. 2018
An old fashioned way:
>> V = [1,0,0,0,2,0,0,0,3,0,0,0,0,0,5,0,0,0,0];
>> X = V~=0;
>> U = V(X);
>> U(cumsum(X))
ans =
1 1 1 1 2 2 2 2 3 3 3 3 3 3 5 5 5 5 5

Weitere Antworten (1)

jonas
jonas am 19 Sep. 2018
A is your array
A(A==0)=NaN
A=fillmissing(A,'previous')
  1 Kommentar
Murugan C
Murugan C am 20 Sep. 2018
currently i am using lower version. I think, it should work for higher version. Thanks for your suggestion.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by