how to index the stating position of something?

조회 수: 3 (최근 30일)
Nourhan
Nourhan 2023년 8월 29일
댓글: Nourhan 2023년 8월 29일
Hi,
I was solving a question that asks to find the length of the largest section of zeros and its starting postion.
Example: x = [1 0 2 3 8 5 6 7 0 0 0 0 0 2 5 3 0 0 6 4]
LP = [5 9]
I was able to find the length, but I coudn't find a way to index the starting postion
my code:
function LP = LengthAndPosnZeros(x)
LP = [0 0];
y = [];
count = 0;
for i = 1:length(x)
if x(i) == 0
count = count+1;
y = horzcat(y,count);
else
count = 0;
end
end
L = max(y)
% P = ? I don't know
LP(1,1) = L
LP(1,2) = P
end

답변 (4개)

Dyuman Joshi
Dyuman Joshi 2023년 8월 29일
The code you have requires a few modifications to work.
Try this -
x1 = [1 0 2 3 8 5 6 7 0 0 0 0 0 2 5 3 0 0 6 4];
out1 = LengthAndPosnZeros(x1)
out1 = 1×2
5 9
x2 = [1 0 0 8 5 0 0 3 3 3 3 0 0 0 0];
out2 = LengthAndPosnZeros(x2)
out2 = 1×2
4 12
x3 = [0 0 0 0 0 0 0 1 99 0];
out3 = LengthAndPosnZeros(x3)
out3 = 1×2
7 1
function LP = LengthAndPosnZeros(x)
d = diff([false,x==0,false]);
b = find(d>0);
e = find(d<0);
[m,idx] = max(e-b);
LP = [m b(idx)];
end
In case, you want to modify your current instead of using the code I provided, please let me know
  댓글 수: 6
dim-ask
dim-ask 2023년 8월 29일
@Nourhan b is the start indices of each zero-segment, and e are the indices where they end (or better, one index more than their last). Then `e-b` are the lengths of each segment.
dim-ask
dim-ask 2023년 8월 29일
@Image Analyst Imo it depends. If you intend to check very large arrays or use the function many times in some loop, then `bwareafilt` is quite (~20x in my machine) slower. It is not optimised for 1d arrays, as its purpose is to be mainly used on general 2d arrays, thus I do not think it is an obvious case to prefer that over constructing one specific for 1d arrays. It is perfectly fine for the scale of the specific array given of course.

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


Heisenberg
Heisenberg 2023년 8월 29일
function LP = LengthAndPosnZeros(x)
LP = [0 0];
y = [];
count = 0;
MaxInd=[];
for i = 1:length(x)
if x(i) == 0
count = count+1;
if i>1 && x(i-1)~=0
MaxInd = horzcat(MaxInd,i)
else
if i==1 && x(i)==0
MaxInd = horzcat(MaxInd,1)
else
MaxInd = horzcat(MaxInd,0)
end
end
y = horzcat(y,count)
else
count = 0;
end
end
try
[L,index] = max(y);
P= MaxInd(index-L+1);
% P = ? I don't know
LP(1,1) = L
LP(1,2) = P
catch
disp('all non zeros')
end
end
This should fix your proble...cheers :)

Torsten
Torsten 2023년 8월 29일
x1 = [1 0 2 3 8 5 6 7 0 0 0 0 0 2 5 3 0 0 6 4];
out1 = LengthAndPosnZeros(x1)
out1 = 1×2
5 9
x2 = [1 0 0 8 5 0 0 3 3 3 3 0 0 0 0];
out2 = LengthAndPosnZeros(x2)
out2 = 1×2
4 12
x3 = [0 0 0 0 0 0 0 1 99 0];
out3 = LengthAndPosnZeros(x3)
out3 = 1×2
7 1
function LP = LengthAndPosnZeros(x)
LP(1) = 0;
LP(2) = 0;
i = 1;
while i <= length(x)
if x(i) == 0
start = i;
len = 1;
for j = i+1:length(x)
if x(j) == 0
len = len + 1;
else
break
end
end
if len > LP(1)
LP(1) = len;
LP(2) = start;
end
i = j;
else
i = i + 1;
end
end
end

Image Analyst
Image Analyst 2023년 8월 29일
If you have the Image Processing Toolbox, why not use the power of the bwareafilt function to immediately extract the longest run of zeros? Everything after that is trivial.
x = [1 0 2 3 8 5 6 7 0 0 0 0 0 2 5 3 0 0 6 4];
x2 = bwareafilt(x == 0, 1) % Get longest run of zeros. x2 will = [0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0]
zeroLength = sum(x2) % Count them.
startingIndex = find(x2, 1, 'first') % Find index of first zero
LP = [zeroLength, startingIndex] % Stitch together into a vector called "LP".
  댓글 수: 3
Image Analyst
Image Analyst 2023년 8월 29일
OK, here is it in only 2 lines.
x2 = bwareafilt(x == 0, 1) % Get longest run of zeros. x2 will = [0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0]
LP = [sum(x2), find(x2, 1, 'first')] % Stitch together into a vector called "LP".
This is far, far simpler than the other complicated and cryptic solutions shown here because this uses the function literally meant for finding the longest run. Can't get any simpler, more obvious, and more intuitive than this.
Nourhan
Nourhan 2023년 8월 29일
Thank you so much. But I want to say that not only I am new to MATLAB, but I am also new to programming in general. So I am still learning how to use loops, conditions and basic functions, that's why I prefer to solve problems using them.

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

카테고리

Help CenterFile Exchange에서 Scripts에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by