How to replace values?
이전 댓글 표시
How can I replace values? I've a matrix of 3653x337 Let's consider I've
- dates values
- 0101 0
- 0102 0
- 0103 0
- 0104 1
- 0105 0
- 0106 0
- 0107 0 ....
I need a function to find all these "1"'s in my matrix and to replace it for two days before and after I've a "1".
Thanks!
댓글 수: 4
Azzi Abdelmalek
2013년 7월 16일
This is not clear
Ugur
2013년 7월 16일
Cedric
2013년 7월 16일
And why not to 0101 and 0107?
the cyclist
2013년 7월 16일
I think he means that if there is a 1 in the second column, put two more 1's before and two more after (all in the second column).
채택된 답변
추가 답변 (1개)
Jos (10584)
2013년 7월 17일
Here's a one-liner:
values = [0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1]
values(max(min(bsxfun(@plus,reshape(find(values==1),[],1),-2:2),numel(values)),1))=1
or in more readable format:
values = [0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1]
oldIndex = find(values==1)
newIndex = bsxfun(@plus, oldIndex(:), -2:2)
newIndex = max(newIndex,1)
newIndex = min(newIndex,numel(values))
values2 = values % work on a copy
values2(newIndex) = 1
Note that you do not need unique numbers for right-hand indexing
B = [0 0 0]
B([2 2 2 2 2 2]) = 1
댓글 수: 3
Ugur
2013년 7월 17일
Jos (10584)
2013년 7월 17일
you should type numel with an lower case L instead of the digit 1 ...
Ugur
2013년 7월 20일
카테고리
도움말 센터 및 File Exchange에서 Library Development에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!