Info
此问题已关闭。 请重新打开它进行编辑或回答。
Changing elements with a condition
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
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'
0 个评论
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
0 个评论
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!