Finding Indices of Zeroes Following Non-Zero Elements in MATLAB
조회 수: 9 (최근 30일)
이전 댓글 표시
Hello everyone,
I am working on a MATLAB project where I have a numeric array, which is a series of numbers including zeroes and non-zero numbers. An example of the sequence is as follows: seq = [0,0,0,0,0,0 (find index),1,3,5,2,4,6,7,8,3,0 (find index),0,0,0 (find index),4,1,10,22,11,22,44,44,12,0 (find index),0,0,0]; In this sequence, I am trying to find the indices of zeroes that come immediately after a non-zero number. In the provided sequence, the output should be [6, 16, 19, 29].
Can you help me to write the code? Thank you
댓글 수: 0
답변 (4개)
Chunru
2023년 6월 30일
x = [0,0,0,0,0,0,1,3,5,2,4,6,7,8,3,0,0,0,0,4,1,10,22,11,22,44,44,12,0,0,0,0];
x1 = x>0 % clipped x
i1 = find(diff(x1)>0)
i2 = find(diff(x1)<0)+1
i = sort([i1 i2])
댓글 수: 0
Harshavardhan Putta
2023년 6월 30일
Hi,
I understand that you are working on a MATLAB project where you have a numeric array consisting of both zero and non-zero numbers. The specific sequence you provided as an example contains various elements, including zeros. Your goal is to find the indices of zeroes that immediately follow a non-zero number in the sequence.
To accomplish this, you plan to iterate through the array and examine each element. If you encounter a non-zero number, you will check the subsequent element to determine if it is zero. If it is, you will record the index of that zero in a separate list.
% Given sequence
seq = [0,0,0,0,0,0,1,3,5,2,4,6,7,8,3,0,0,0,4,1,10,22,11,22,44,44,12,0,0,0];
% Find indices of zeroes after a non-zero number
indices = [];
for i = 1:length(seq)-1
if seq(i) ~= 0 && seq(i+1) == 0
indices = [indices, i+1];
end
end
disp(indices);
I hope it helps!
댓글 수: 0
Anamika
2023년 7월 7일
Here's an example code that finds the indices of zeroes that finds the indices of zeroes that come immediately after a non-zero number in a given sequence:
seq = [0,0,0,0,0,0,1,3,5,2,4,6,7,8,3,0,0,0,4,1,10,22,11,22,44,44,12,0,0,0];
%indices of non-zero numbers
nonZeroIndices = find(seq ~= 0);
%indices of zeroes that come immediately after a non-zero number
zeroIndices = nonZeroIndices(find(diff(nonZeroIndices) == 1)) + 1;
disp(zeroIndices);
What this code is evaluating?
This code first finds the indices of non-zero numbers in the sequence using the find function, to find the difference between consecutive non-zero indices can use diff function, by comparing these differences to 1 can identify the indices of zeroes that come immediately after a non-zero number after this finally, the code adds 1 to these indices to obtain the desired output.
The output of the above code will be: 6 16 19 29
댓글 수: 0
Bruno Luong
2023년 7월 7일
seq = [0,0,0,0,0,0,1,3,5,2,4,6,7,8,3,0,0,0,0,4,1,10,22,11,22,44,44,12,0,0,0,0]
b = seq ~= 0;
union(findstr(b, [0 1]), findstr(b, [1 0])+1)
댓글 수: 1
Bruno Luong
2023년 7월 7일
A variation
seq = [0,0,0,0,0,0,1,3,5,2,4,6,7,8,3,0,0,0,0,4,1,10,22,11,22,44,44,12,0,0,0,0]
d = diff(seq ~= 0);
union(find(d==1), find(d==-1)+1)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!