Hi,
I am new to Matlab and i am trying to decrement a simple array. I want every element to arrives at zero and no negative. The problem is once any element of the array arrives at zero the decrement stops, i have tried to use several combination but nothing works. It goes like this
n=[8 5 9 3 6 11]
if n~=0
n=n-1
end.
As soon as the the value '3' arrives at zero the decrement stops. I have also used "sum" but it didn't work either. For instance while sum~=0, do the decrement but it didn't work either. I know its a simple task but i am stuck.

1 comentario

Mustafa
Mustafa el 11 de Jul. de 2011
Just for future reference.
The answer by "the cyclist" is also correct. Personally it suits me better.

Iniciar sesión para comentar.

 Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 11 de Jul. de 2011

0 votos

?
Ds = cumsum([n;-ones(max(n),length(n))]);
Ds(Ds<0)=0

1 comentario

Mustafa
Mustafa el 11 de Jul. de 2011
Thats it. Thanks a trillion.
I thought there must be simple "if" or "while" statement to do this but thats good for now.
Appreciate it.
P.S: Is there any way to stop the decrement once i found that one elment of the array arrives at zero. I need to perform some operation and than continue with the decrement.

Iniciar sesión para comentar.

Más respuestas (3)

the cyclist
the cyclist el 11 de Jul. de 2011

2 votos

Edited in response to your comment:
while not(all(n==0))
n = max(0,n-1)
end
Wanted to keep this all in one answer. I have edited my response again, but left the old one. In the code below, I have inserted an if statement that will be entered only when an element of n has just been decremented to zero, as you mentioned you wanted in a comment on andrei's answer.
(I also implemented Jan's simpler syntax for the while loop.)
n0 = [8 5 9 3 6 11];
n = n0;
iter = 0;
while any(n)
iter = iter+1;
n = max(0,n-1)
if any(iter==n0);
% If I am here, it is because one of the original element just reached zero
% Do stuff
end
end

4 comentarios

Mustafa
Mustafa el 11 de Jul. de 2011
Thanks, but i want every element of the array to arrives at zero. If let say "3" becomes zero in third iteration, it will remain zero until "11" arrives at zero.
Jan
Jan el 11 de Jul. de 2011
not(all(n==0)) is equivalent to any(n).
Mustafa
Mustafa el 11 de Jul. de 2011
Thanks a lot...works as i really want it to work. Appreciate it :)
Mustafa
Mustafa el 12 de Jul. de 2011
Thanks again for help. Really appreciate it. Well in my case i have to assign the " particular element" that become zero another value. I have done it like this
for i=1:40 % Just a loop so that the iteration will keep on running
while any(n)
n=max(0,n-1)
n_indx=find(n==0)
n(n_indx)=20 % Let say i have to assign that element a value of 20.
break
end
end
There is problem if you run the above code, the decrement happens after every second step. Please check the output to understand it more clearly.

Iniciar sesión para comentar.

Jan
Jan el 11 de Jul. de 2011

2 votos

See ALL and ANY:
n=[8 5 9 3 6 11]
if all(n~=0) % Equivalent: if all(n)
n = n-1
end

1 comentario

Mustafa
Mustafa el 11 de Jul. de 2011
Your code stops after two iterations,
I want every element of the array to arrives at zero. If let say "3" becomes zero in third iteration, it will remain zero until "11" arrives at zero.

Iniciar sesión para comentar.

Sean de Wolski
Sean de Wolski el 11 de Jul. de 2011

0 votos

n = n-min(n(:));
?
And the way to fix your if statement is to use any as the criterion.
while ~any(n==0)
n = n-1;
end

8 comentarios

Mustafa
Mustafa el 11 de Jul. de 2011
Thanks, but i want every element of the array to arrives at zero. If let say "3" becomes zero in third iteration, it will remain zero until "11" arrives at zero.
Sean de Wolski
Sean de Wolski el 11 de Jul. de 2011
n = zeros(size(n))
Mustafa
Mustafa el 11 de Jul. de 2011
Can you write the whole code, i am sorry i couldn't figure it out.
Sean de Wolski
Sean de Wolski el 11 de Jul. de 2011
Well you want to decrement every value to zero so you'll just have an array of values. Why waste the time and effort doing the math - just define it as an array of zeros.
Sean de Wolski
Sean de Wolski el 11 de Jul. de 2011
while any(~n)
n(~n) = n(~n)-1;
end
is the mathy way to do it
Mustafa
Mustafa el 11 de Jul. de 2011
:).
Well i need to decrement the array till zero and if one of the element arrives at zero, i need to perform some operation and than continue to decrement the array again. The element that become zero will get a new random number after some time. It goes on like this.
But thanks anyways :)
Bheki Ngobe
Bheki Ngobe el 14 de Feb. de 2016
Hi
I have kind of similar problem but I am using 3x4 array. How should I do it in this situation. In brief I want to apply an formula in each cell and increment to the next one until all cells are updated. Please help
the cyclist
the cyclist el 15 de Feb. de 2016
You should probably ask a new question, rather than add a comment to a 4-year-old question.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 11 de Jul. de 2011

Comentada:

el 15 de Feb. de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by