フィルターのクリア

Replace number with text

27 ビュー (過去 30 日間)
stelios loizidis
stelios loizidis 2021 年 9 月 23 日
回答済み: Walter Roberson 2021 年 9 月 23 日
Hello, I have the following problem. I have a matrix A which has dimensions of 300x350. In this matrix I want to replace the value 5 with the phrase "NO". I wrote a small code but it does not replace. I attach the code.
% finding positions of number 5 from table A.
[i1,k1]=find(A==5);
% Replace value 5 with text "NO"
for i=1:length(i1)
for j=1:length(k1)
if A==5
A(i1(i),k1(j))=disp('NO');
end
end
end
Your help is invaluable.
  1 件のコメント
Chunru
Chunru 2021 年 9 月 23 日
編集済み: Chunru 2021 年 9 月 23 日
Matrix or array in matlab must have the same data type. You can not mix the double data with a string 'NO' in same matrix.

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

採用された回答

KSSV
KSSV 2021 年 9 月 23 日
編集済み: KSSV 2021 年 9 月 23 日
Instead of a string, you can try replacing it with NaN.
% finding positions of number 5 from table A.
[i1,k1]=find(A==5);
% Replace value 5 with text "NO"
for i=1:length(i1)
for j=1:length(k1)
if A==5
A(i1(i),k1(j))=NaN ;
end
end
end
Also you need not tuse a loop.
idx = A == 5 ;
A(idx) = NaN ;
  1 件のコメント
stelios loizidis
stelios loizidis 2021 年 9 月 23 日
It works!!!!
Thanks for the valuable help.

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2021 年 9 月 23 日
Adjust to suit
A = magic(7);
newA = num2cell(A);
mask = mod(A,5) == 0;
newA(mask) = {'NO'};
newA
newA = 7×7 cell array
{'NO'} {[39]} {[48]} {[ 1]} {'NO'} {[19]} {[28]} {[38]} {[47]} {[ 7]} {[ 9]} {[18]} {[27]} {[29]} {[46]} {[ 6]} {[ 8]} {[17]} {[26]} {'NO'} {[37]} {'NO'} {[14]} {[16]} {'NO'} {[34]} {[36]} {'NO'} {[13]} {'NO'} {[24]} {[33]} {[42]} {[44]} {[ 4]} {[21]} {[23]} {[32]} {[41]} {[43]} {[ 3]} {[12]} {[22]} {[31]} {'NO'} {[49]} {[ 2]} {[11]} {'NO'}

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by