フィルターのクリア

Is it possible to put a character into a matrix?

19 ビュー (過去 30 日間)
Recap
Recap 2016 年 4 月 1 日
回答済み: Image Analyst 2016 年 4 月 1 日
I'm trying to do this
m1 = [1, 2, 3, 4, 5, 6, 7];
m2 = 'A';
m1(3) = m2;
so that m1 will be
[1, 2, A, 4, 5, 6, 7]
Instead of what I actually get.
[1, 2, 65, 4, 5, 6, 7]

採用された回答

Image Analyst
Image Analyst 2016 年 4 月 1 日
You'd have to use a cell array. I couldn't figure out how to get cell2mat() to work, so I used a for loop:
m1 = [1, 2, 3, 4, 5, 6, 7];
m2 = 'A';
for k = 1 : length(m1)
m3{k} = m1(k);
end
% m3 = mat2cell(m1, ones(1, 7)) %, ones(1, length(m1)), numel(m1))
celldisp(m3)
% Now replace third element with a character.
m3{3} = m2;
celldisp(m3)

その他の回答 (1 件)

Star Strider
Star Strider 2016 年 4 月 1 日
You cannot mix numeric and character values in a numeric array. You can do it with a cell array, but it’s only good for storing the values. You would encounter the same problem if you wanted to calculate with it.

カテゴリ

Help Center および File ExchangeMultidimensional Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by