Hello Matlab community!
Could someone please give me a hand with my code?
I've come up with an array such as
A = [0, 0, 0, 0, 0, 0, 4, 5, 6, 9, 4, 3, 9, 0, 0, -1, -1, -1, -1, 0, 0, 3, 2, 8, 3, 0, -1, 0, -1, 0, -1, -1, 0, 0, 5, ..., n]
The main idea is adding A(n) values when A(n) is 0 or positive.
But when A(n) is -1, I'd like it to subtract the sum of the previous positions into equal parts (1/4) to get zero.
(Note: there are always four -1 before a positive value, so the sum should be divided by 4). The output should be as follows:
Output = [0, 0, 0, 0, 0, 0, 4, 9, 15, 24, 28, 31, 40, 40, 40, 30, 20, 10, 0, 0, 0, 3, 5, 13, 16, 16, 12, 12, 8, 8, 4, 0, 0, 0, 5,..., n]
I hope I've explained myself clear enough for you to understand.
Thank's for the help!

 Respuesta aceptada

Timo Dietz
Timo Dietz el 4 de En. de 2021
decrA = 0;
out = zeros(1, numel(A));
out(1) = A(1);
for idx = 2:1:numel(A)
if A(idx) == -1
out(idx) = out(idx - 1) - decrA;
else
out(idx) = out(idx - 1) + A(idx);
decrA = out(idx)/4;
end
end
out
Does this solve your issue?

6 comentarios

Santos García Rosado
Santos García Rosado el 5 de En. de 2021
Seems like it works just as it should. Thank's for your time Timo!
Santos García Rosado
Santos García Rosado el 11 de En. de 2021
Hello Timo. I've been checking the code and it seems like it won't work for a particular scenario.
When there aren´t spaces between the -1 posotions the code works just fine. The problem is when there are spaces between those positions. I haven't figured a solution out yet. Any ideas?
Thank you!
Timo Dietz
Timo Dietz el 11 de En. de 2021
What does "spaces" mean. I interpreted "(Note: there are always four -1 before a positive value, so the sum should be divided by 4)." as there are always four -1 in a row.
Please provide an example and the expected output.
Sorry I didn't make myself clear.
So for vector A:
A = [0, 0, 0, 0, 0, 0, 4, 5, 6, 9, 4, 3, 9, 0, 0, -1, -1, -1, -1, 0, 0, 3, 2, 8, 3, 0, -1, 0, -1, 0, -1, -1, 0, 0, 5];
The expected Output should be:
Output = [0, 0, 0, 0, 0, 0, 4, 9, 15, 24, 28, 31, 40, 40, 40, 30, 20, 10, 0, 0, 0, 3, 5, 13, 16, 16, 12, 12, 8, 8, 4, 0, 0, 0, 5]
However, the output we get with your code is:
Out = [0, 0, 0, 0, 0, 0, 4, 9, 15, 24, 28, 31, 40, 40, 40, 30, 20, 10, 0, 0, 0, 3, 5, 13, 16, 16, 12, 12, 9, 9, 6.75, 4.5, 4.5, 4.5, 9.5]
As you can see your code works perfectly well when there are four -1 in a row (first part of A). On the other hand, when the -1 values are not in a row the code won't be useful.
I hope I've explained my self better now.
Any ideas? Thank you!
decrA = 0;
out = zeros(1, numel(A));
out(1) = A(1);
countMinusOnes = 1;
for idx = 2:1:numel(A)
if A(idx) == -1
out(idx) = out(idx - 1) - decrA;
countMinusOnes = countMinusOnes + 1;
if countMinusOnes > 4; countMinusOnes = 1; end
else
out(idx) = out(idx - 1) + A(idx);
if countMinusOnes == 1; decrA = out(idx)/4; end
end
end
Okay, you have to make sure that all four '-1' have been there, before calculating a new decrement, right?
My proposal is not very elegant but maybe it solves your issue.
What in case there are values >0 between the -1? The code here would further add these but leaves the decrement as is. Would that be okay?
Santos García Rosado
Santos García Rosado el 11 de En. de 2021
Now works perfectly! Don't worry about having values >0 between the -1 positions, that scenario won't come along in my program. Thank you so much for your time Timo!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Productos

Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by