Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

Changing elements with a condition

1 visualización (últimos 30 días)
ajk1
ajk1 el 17 de Ag. de 2016
Cerrada: MATLAB Answer Bot el 20 de Ag. de 2021
Hello, I have a binary variable (size 733x1) called ' column ' and in order to change the 0's in-between where I have 1's to 1's (i.e. 00001110011... to 00001111111...). I am using:
column_fill = column;
column_fill(find(column == 1, 1):find(column == 1, 1, 'last')) = 1;
I would like help altering this for a different section of my code so when it finds the last '1' to change all previous '0' values to '1'. For example (000...00011010000 to 000...00011111111). Thanks.
  1 comentario
Azzi Abdelmalek
Azzi Abdelmalek el 17 de Ag. de 2016
You can attach your file in this forum

Respuestas (2)

Azzi Abdelmalek
Azzi Abdelmalek el 17 de Ag. de 2016
Editada: Azzi Abdelmalek el 17 de Ag. de 2016
s='00000011010000'
ss='1'
out=regexprep(s,'(1.+)','${repmat(ss,1,numel($1))}')
%or simply
s='00000011010000'
idx=find(s=='1',1)
s(idx:end)='1'

Image Analyst
Image Analyst el 17 de Ag. de 2016
Try this:
% Change (000...00011010000 to 000...00011111111). Thanks.
v = [0,0,0,0,0,0,1,1,0,1,0,0,0,0]
% Find next to the last 1
indexes = find(v, 2, 'last')
% Set from there to the end to be 1
v(indexes(1):end) = 1
Note: what you said and gave as a result are contradictory. The code above doesn't change ALL previous 0's to 1's like you said, it just replaces the next to the last run of zeros, plus the last run of 0's with 1's like you gave as the numerical desired answer. If you want ALL prior 0's to be 1's, then find the very last 1 and change everything up to that point to be 1:
% Change (000...00011010000 to 111...11111110000). Thanks.
v = [0,0,0,0,0,0,1,1,0,1,0,0,0,0]
% Find the last 1
indexOfLast1 = find(v, 1, 'last')
% Set from there to the end to be 1
v(1:indexOfLast1) = 1

La pregunta está cerrada.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by