필터 지우기
필터 지우기

array generation using logics.

조회 수: 1 (최근 30일)
Sathiya S V
Sathiya S V 2024년 5월 29일
댓글: Voss 2024년 5월 29일
x = [0 10 20 30 0 10 20 30 40 0 10 20 30 40 50 0 10]
'x' is the array to be checked.
checked = [0 10 -10 20 0 10 -10 20 -20 0 10 -10 20 -20 30 0 10]
I want the new array like the above mentioned 'checked' array.
The concept is simple to understand in the above given example, if the numbers are countinously increasing in the order of 10 factors in the x array, I need a checked array of incrementing values containing positive and negative number and next should be a next consecutive number in positive sign.
It is simple for me to understand, but for coding I really finding it difficult..I request someone who is more into finding logics to help me with.

채택된 답변

Voss
Voss 2024년 5월 29일
x = [0 10 20 30 0 10 20 30 40 0 10 20 30 40 50 0 10];
diffx = diff(x(:));
start = find([true; diffx <= 0; true]);
dx = min(diffx(diffx > 0));
checked = zeros(size(x));
sequence_length = diff(start);
for ii = 1:numel(sequence_length)
n = 1:sequence_length(ii);
checked(start(ii):start(ii+1)-1) = ceil((n-1)/2)*dx.*(-1).^n;
end
disp(checked);
0 10 -10 20 0 10 -10 20 -20 0 10 -10 20 -20 30 0 10
  댓글 수: 2
Sathiya S V
Sathiya S V 2024년 5월 29일
Thanks, it works:)
Voss
Voss 2024년 5월 29일
You're welcome!

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

추가 답변 (1개)

Dyuman Joshi
Dyuman Joshi 2024년 5월 29일
x = [0 10 20 30 0 10 20 30 40 0 10 20 30 40 50 0 10]
x = 1x17
0 10 20 30 0 10 20 30 40 0 10 20 30 40 50 0 10
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
y = x;
idx = [1 find(diff(x)~=10)+1 numel(x)]
idx = 1x5
1 5 10 16 17
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
for k=1:numel(idx)-1
vec = (1:idx(k+1)-idx(k)-1);
y(vec+idx(k)) = ceil(vec/2).*10.*(-1).^(vec-1);
end
y
y = 1x17
0 10 -10 20 0 10 -10 20 -20 0 10 -10 20 -20 30 0 10
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by