필터 지우기
필터 지우기

how to shift an array value upward ?

조회 수: 2 (최근 30일)
preet
preet 2013년 4월 2일
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
  댓글 수: 4
Azzi Abdelmalek
Azzi Abdelmalek 2013년 4월 2일
you added a value to what?
preet
preet 2013년 4월 2일
i m adding value with array itself if the current value of array is less or equal to 15.

댓글을 달려면 로그인하십시오.

채택된 답변

Mahdi
Mahdi 2013년 4월 2일
Just use
a=a(1:end-1)
to remove the last value.

추가 답변 (1개)

Jan
Jan 2013년 4월 2일
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
  댓글 수: 2
preet
preet 2013년 4월 2일
simon i want to a[ ] array again not r [].
Jan
Jan 2013년 4월 2일
편집: Jan 2013년 4월 2일
@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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by