Asked by preet
on 2 Apr 2013

a=[10,14,5,6,7,19]

in this array need to build an array a[] again but the value should not be less than or equal to 15 i need it

a=[24,18,19]

when i shift the value after adding my value which i have already added remain in the array like this

in 1st loop i have result lke this

a=[24,5,6,7,19,19]

how to get the right result with shifting

*No products are associated with this question.*

Answer by Mahdi
on 2 Apr 2013

Accepted answer

Just use

a=a(1:end-1)

to remove the last value.

Answer by Jan Simon
on 2 Apr 2013

a = [10, 14, 5, 6, 7, 19]; r = zeros(size(a)); % Pre-allocate maximal length ri = 0; q = 0; for k = 1:length(a) q = q + a(k); % Accumulate values of "a" if q >= 15 % Flush accumulated value: ri = ri + 1; r(ri) = q; q = 0; end end r = r(1:ri); % Crop unused values

Jan Simon
on 2 Apr 2013

@kpreet: Is this a serious question? What about adding this line at the end:

a = r;

Or you can do it "inplace", but the modifications are such tiny (changing all "r" to "a" and "ri" to "ai"), that I'd actually assume, that you could solve this by your own:

a = [10, 14, 5, 6, 7, 19]; ai = 0; q = 0; for k = 1:length(a) q = q + a(k); % Accumulate values of "a" if q >= 15 % Flush accumulated value: ai = ai + 1; a(ai) = q; q = 0; end end a = a(1:ai); % Crop unused values

It would be a kind idea to explain, if at least the values of "r" match your expectations.

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi test

Learn moreOpportunities for recent engineering grads.

Apply Today
## 4 Comments

## Azzi Abdelmalek (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/69479#comment_140618

This is not clear

## preet (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/69479#comment_140619

ok i have an array a[10,14,5,6,19] i add the value which less then 15 or equal to and shift one position upward to the the remain value of array a[]. i got this array after 1st loop a=[24,5,6,7,19,19]

which value shift upward also occupy the same position and going to one position upward. i need to avoid the last position which also going to upward .

but i require a=[24,5,6,7,19]

## Azzi Abdelmalek (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/69479#comment_140643

you added a value to what?

## preet (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/69479#comment_140652

i m adding value with array itself if the current value of array is less or equal to 15.