Changing values in matrix under certain condition

x = 0:50;
y = 0:20;
[X,Y] = ndgrid(x,y);
XY = [X(:), Y(:)];
R=5;
AB=[((X(:)-25).^2 + (Y(:)-10).^2).^0.5];
Displacement = permute(reshape(AB.',(compwidth+1),(compdepth+1),[]),[2 1 3])-R
How can I change the values of the matrix which meet certain condition into another specific number for further calculation?
if Displacement(:,:)<=0
%that specific value become 0
else
%values remain unchange
end

 採用された回答

Jan
Jan 2021 年 3 月 13 日
編集済み: Jan 2021 年 3 月 13 日

0 投票

Displacement = max(Displacement, 0);
Notes:
  • sqrt(x) is 50% faster than x^0.5
  • [ ] is the Matlab operator for a concatenation. In this line, you concatenate a vector with nothing, so omit the square bracket to save time:
AB=[((X(:)-25).^2 + (Y(:)-10).^2).^0.5];
% Faster:
AB = sqrt((X(:)-25).^2 + (Y(:)-10).^2);

1 件のコメント

Yuk Wai Kelvin Chow
Yuk Wai Kelvin Chow 2021 年 3 月 13 日
Thank you very much, it helps me a lot on my project.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeSpline Postprocessing についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by