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 CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by