How can i find 4 continuous component in row and replace

I have a row,
I = [60 60 0 0 0 0 45 60 90];
% I want to find 4 continuous component, and 4th component should be replaced with next component from 4th component. Means, 4th component is 0 which is found in column 6, then it should be replace with column 6th i.e 45,
%Just tell me how to find index, i.e I(6).
New_I = [60 60 0 0 0 45 0 60 90];

4 Commenti

Triveni
Triveni il 6 Ago 2016
Modificato: Triveni il 6 Ago 2016
4th zero i.e last component of zero, if continuous component(0) is found in I(6) or I(k) then replace it with I(7) or I(k+1). I(7) shift to I(6) and I(6) shift to I(7).
But you say
New_I = [60 60 0 0 0 45 0 60 90];
OK, so the 4th zero at index 6 gets replaced with 45, but why did an extra zero get inserted at index 7 and the 60 and 90 get shifted over? I don't understand the rule you're using. And what happened to the first 45? All you have is the new 45 that replaced the last 0 - what happened to the original 45 at index 7? What that replaced by a zero? Or you just deleted it and shifted the last zero over one space to the right? What you say doesn't seem to agree with your result.
I = [60 60 0 0 0 0 45 60 90]; % 9 columns in I and 4th 0 is in 6th column
% I want to replace column 6 or 4th (0) with 45 or column (7).
New_I = [60 60 0 0 0 45 0 60 90];
Because i need to all component group should be present in the row, but continuously no any component more than 4.
Clear as mud. Too bad because I'm quitting for the day. See my comment above (again), my answer below, and look up regionprops() in the help, particularly the PixelIdxList property.

Accedi per commentare.

Risposte (1)

Which zero got replaced? It just looks like you inserted a 45 between the 3rd and 4th zero but there are still 4 zeros like in your original vector.
To find 4 zeros in a row, use bwpropfilt():
I = [60 60 0 0 0 0 45 60 90];
props = bwpropfilt(I == 0, 'Area', 4)
% Now props will = [0 0 1 1 1 1 0 0 0]
% Use that however you want - you're not clear.
% For example
I_new = I; % Initialize
% Replace those zeros with 45's
I_new(props) = 45

2 Commenti

Triveni
Triveni il 8 Ago 2016
Modificato: Walter Roberson il 8 Ago 2016
Please just tell me how to find index of 4th zero i.e I(1,6,1).
zidx = find(I==0, 4, 'first');
if length(zidx) < 4
error('I does not have at least 4 zeros');
end
pos_of_4th_zero = zidx(4);

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Tag

Non è stata ancora inserito alcun tag.

Richiesto:

il 6 Ago 2016

Community Treasure Hunt

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

Start Hunting!

Translated by