How to replace a number with 0 in an array
古いコメントを表示
How to replace a number in a column with 0? I want to replace all 0.5 values in a column with 0
採用された回答
その他の回答 (1 件)
Jos (10584)
2016 年 2 月 12 日
A = [1 2 3 ; 0.5 0.5 2 ; 0.5 5 6] % example data
C = A(:,1)
tf = C == 0.5
A(tf,1) = 0
% or in one line:
% A(A(:,1)==0.5,1) = 0
5 件のコメント
Lilja Dahl
2016 年 2 月 12 日
Lilja Dahl
2016 年 2 月 12 日
dpb
2016 年 2 月 12 日
Just replace the RHS of the assignment with whatever is wanted; only restriction is you'll have to have saved the logical vector to known how many elements are needed to generate the proper number for the assignment. A constant is propagated automagically across any size target; multiple assignment on RHS must be conformant in size to the target on the LHS.
vanam sindhuja
2020 年 9 月 7 日
Can I know the significance of tf.
...
tf = C == 0.5
A(tf,1) = 0
...
tf is the logical addressing vector; the variable name chosen to represent true|false as indicator it is a logical array.
As a subscripting expression, MATLAB returns only the locations in the referenced array for which elements in the indexing expression return TRUE
カテゴリ
ヘルプ センター および File Exchange で Variables についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!