Cell concatenation error with cells with values below 1 and above 1

1 回表示 (過去 30 日間)
Christina Huynh
Christina Huynh 2021 年 11 月 5 日
編集済み: DGM 2021 年 11 月 6 日
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
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
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)
Output = 5×7 char array
'0.10297' '0.54312' '0.98275' '1.4219 ' '1.8605 '
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{:})
ans = 5×7 char array
'0.10297' '0.54312' '0.98275' '1.42190' '1.86050'

DGM
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'}
a = 5×1 cell array
{'0.10297'} {'0.54312'} {'0.98275'} {'1.4219' } {'1.8605' }
If you want strings instead:
% string vector
b = string(a)
b = 5×1 string array
"0.10297" "0.54312" "0.98275" "1.4219" "1.8605"
If you want actual numeric data that you can calculate with:
% numeric vector
b = str2double(a)
b = 5×1
0.1030 0.5431 0.9828 1.4219 1.8605
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
b = NaN
c = ismember('0.10297',a) % ismember() won't do what you expect either
c = 1×7 logical array
1 1 1 1 1 1 1
d = strcmp('0.10297',a) % neither will strcmp()
d = logical
0

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by