Effacer les filtres
Effacer les filtres

Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

How can I make all the variables in an array 0 after the first time 0 occurs in the array

1 vue (au cours des 30 derniers jours)
Nick Pera
Nick Pera le 20 Jan 2016
Clôturé : MATLAB Answer Bot le 20 Août 2021
I am trying to create a weighting function based on a set of criteria. When the weighting function is created, I get an array containing 1's and 0's. However, I am trying to force the array to be all 0's once the first 0 occurs.
For example: x = [1,1,1,1,1,0,0,1,1,0,0,0...] for which I would then like it to read x = [1,1,1,1,1,0,0,0,0,0,0,0...].
Any tips would be greatly appreciated.

Réponses (2)

Guillaume
Guillaume le 20 Jan 2016
A very simple solution that only works with an array of 0 and 1 starting with 1:
x = [1,1,1,1,1,0,0,1,1,0,0,0]
cumprod(x)
  2 commentaires
Walter Roberson
Walter Roberson le 21 Jan 2016
I find in practice that most of the time that I want to do this, I want to work row by row; in that case I need to be sure to specify the dimension number,
cumprod(x,2)
Stephen23
Stephen23 le 21 Jan 2016
Without a leading one:
>> x = [0,0,1,1,1,1,1,0,0,1,1,0,0,1,0]
x =
0 0 1 1 1 1 1 0 0 1 1 0 0 1 0
>> x .* (1==cumsum(0<diff([0,x])))
ans =
0 0 1 1 1 1 1 0 0 0 0 0 0 0 0

jgg
jgg le 20 Jan 2016
Generate your x as normal, with the many 1's afterwards then call:
x = [1,1,1,1,1,0,0,1,1,0,0,0]; %generate x
x(find(x == 0,1,'first'):end) = 0
  2 commentaires
Nick Pera
Nick Pera le 20 Jan 2016
great!! worked perfectly. thank you!
jgg
jgg le 21 Jan 2016
Accept this answer if it solved your problems so other can use it!

Cette question est clôturée.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by