Cell concatenation error with cells with values below 1 and above 1
1 回表示 (過去 30 日間)
古いコメントを表示
a = {'0.10297';'0.54312';'0.98275';'1.4219';'1.8605'}
This is the array that I was to verticaly concatenate. However, when I try use vertcat, I got the "Dimensions of arrays being concatenated are not consistent." error. I tried changing the numbers and realized that this was because most entries are below 1 and 2 of them was above 1. When I removed the entry '1.4219' and '1.8605', there was no error.
What can I do to verticaly concatenate a without splitting it up?
1 件のコメント
DGM
2021 年 11 月 5 日
Those aren't numbers. They're character vectors. You can't vertically concatenate row vectors of different length, as arrays must be rectangular.
Are these supposed to be char vectors, or are they supposed to be numeric?
回答 (2 件)
C B
2021 年 11 月 6 日
You can use char command @Christina Huynh
a = {'0.10297';'0.54312';'0.98275';'1.4219';'1.8605'};
Output = char(a)
And it was not because of it was above one it was because size of eac element was different
if you make '1.4219' = '1.42190' and '1.8605' = '1.86050', vertcat wil work..
a = {'0.10297';'0.54312';'0.98275';'1.42190';'1.86050'};
vertcat(a{:})
0 件のコメント
DGM
2021 年 11 月 6 日
編集済み: DGM
2021 年 11 月 6 日
Again, the question is whether you want to treat these as text or as numeric values for subsequent calculations. If you want them as text, they're already in a usable form. This is exactly how you would hold multiple char vectors.
% cell vector of char vectors
a = {'0.10297';'0.54312';'0.98275';'1.4219';'1.8605'}
If you want strings instead:
% string vector
b = string(a)
If you want actual numeric data that you can calculate with:
% numeric vector
b = str2double(a)
While there are occasional conveniences in making 2D character matrices, it's not common. Many functions already expect text to be formatted as cell arrays of chars or as string arrays. You will probably find that most functions won't treat a 2D char array as you expect.
a = {'0.10297';'0.54312';'0.98275';'1.42190';'1.86050'};
a = vertcat(a{:}); % 2D char array
b = str2double(a) % str2double() can't use this
c = ismember('0.10297',a) % ismember() won't do what you expect either
d = strcmp('0.10297',a) % neither will strcmp()
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Creating and Concatenating Matrices についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!