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!