Info
This question is closed. Reopen it to edit or answer.
Changing elements with a condition
1 view (last 30 days)
Show older comments
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 Comment
Answers (2)
Azzi Abdelmalek
on 17 Aug 2016
Edited: Azzi Abdelmalek
on 17 Aug 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'
0 Comments
Image Analyst
on 17 Aug 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
0 Comments
This question is closed.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!