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!