Finding Indices of Zeroes Following Non-Zero Elements in MATLAB
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (4)
Chunru
am 30 Jun. 2023
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 Kommentare
Harshavardhan Putta
am 30 Jun. 2023
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 Kommentare
Anamika
am 7 Jul. 2023
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 Kommentare
Bruno Luong
am 7 Jul. 2023
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 Kommentar
Bruno Luong
am 7 Jul. 2023
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)
Siehe auch
Kategorien
Mehr zu Function Creation finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!