Compare two vector arrays and if they match set vector array 2 = nan?
1 次查看(过去 30 天)
显示 更早的评论
So i have two arrays the same size:
time = [0,0,0,0,2,3,4,5,5,6,7,8]';
solution = [1,2,0,0,1,1,2,2,2,0,0,0];
What would be the best way to compare the two and if both are the same make solution 0 = nan;
So the final outcome i would like is solution = [1,2,nan,nan,1,1,2,2,2,0,0,0];
would i use a function such as:
if strfind(time,solution)==0
Do something?
Thanks in adavnce!
0 个评论
采纳的回答
Walter Roberson
2022-2-11
time = [0,0,0,0,2,3,4,5,5,6,7,8]
solution = [1,2,0,0,1,1,2,2,2,0,0,0]
outcome = solution;
outcome(solution == 0 & time == 0) = nan
It is not clear what you want to have happen in positions where the values are equal, but the values are not 0.
3 个评论
Walter Roberson
2022-2-11
solution(time ==0 & solution==0) = nan;
if ~any(isnan(solution)); return; end
更多回答(1 个)
Arif Hoq
2022-2-11
编辑:Arif Hoq
2022-2-11
Try this...
time = [0,0,0,0,2,3,4,5,5,6,7,8];
solution = [1,2,0,0,1,1,2,2,2,0,0,0];
[idx]=find(time==solution);
solution(idx)=[NaN NaN]
2 个评论
Stephen23
2022-2-11
FIND is not required, logical indexing is simpler and more efficient.
It would be better to allocate a scalar NaN.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!