How to replace a double in a array with a string (cell)?

7 ビュー (過去 30 日間)
Seungeun Lee
Seungeun Lee 2015 年 10 月 2 日
コメント済み: Walter Roberson 2015 年 10 月 3 日
I have a matrix of alternating 1s and 0s. Such as A = 1 0 1 0 1 0 0 1 0 1 0 1
I want to replace the "1" with a string called "[apple]" including the brackets.
How should I do this? It keeps giving me errors.

回答 (2 件)

Walter Roberson
Walter Roberson 2015 年 10 月 2 日
A = [1 0 1 0 1 0 0 1 0 1 0 1];
Ac = num2cell(A);
Ac(A==1) = {'[apple]'};
If you wanted to replace 1 with [apple] and 0 with something else you could do
str_table = {'[orange]', '[apple]'};
Ac = str_table(A+1);

Andrei Bobrov
Andrei Bobrov 2015 年 10 月 2 日
A = [1 0 1 0 1 0 0 1 0 1 0 1];
out = cell(size(A));
out(A>0) = {'[apple]'};
  1 件のコメント
Walter Roberson
Walter Roberson 2015 年 10 月 3 日
This would leave empty cells where the 0 should be.

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

カテゴリ

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