Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Changing elements with a condition

조회 수: 1 (최근 30일)
ajk1
ajk1 2016년 8월 17일
마감: MATLAB Answer Bot 2021년 8월 20일
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
Azzi Abdelmalek
Azzi Abdelmalek 2016년 8월 17일
You can attach your file in this forum

답변 (2개)

Azzi Abdelmalek
Azzi Abdelmalek 2016년 8월 17일
편집: Azzi Abdelmalek 2016년 8월 17일
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 2016년 8월 17일
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

이 질문은 마감되었습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by