Replace Elements in matrix in specific positions
1 次查看(过去 30 天)
显示 更早的评论
Hello, i have 3 matrixes of 15001 elements, A, B and C . What i want is that when A<=1.2 and in the same positions, -0.54<=B<=0.54, the same position D will be 0 , else inmatrix D will be = C.
0 个评论
回答(1 个)
goerk
2019-6-6
编辑:goerk
2019-6-6
mask = A<=1.2 && B>-0.54 && B<0.54;
D=zeros(size(C));
D(mask)=C(mask);
Edit:
Range adapted.
2 个评论
Steven Lord
2019-6-6
Replace the && in your answer with &. Use && for short-circuiting when the two expressions you're trying to and together are scalars. Use & to and together two arrays element-wise.
% Sample data
A = randn(5);
B = randn(5);
% Will throw an error
mask = A<=1.2 && B>-0.54 && B<0.54;
% Will not throw an error
mask = A<=1.2 & B>-0.54 & B<0.54;
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!