how to convert vector char to matrix char ?

34 次查看(过去 30 天)
I have a variable X = 'ABCDEFGHIJKLMNOPQRSTWXYZ'
how i can fill it in a matrix with single char in single index
like X(1,1)=A ,X(1,2)=B,X(1,3)=C and so on.
  1 个评论
Stephen23
Stephen23 2021-9-13
Using a cell array for this is pointlessly complex and inefficient.
Using a character array, as Chunru shows, is simpler and more efficient.

请先登录,再进行评论。

采纳的回答

Chunru
Chunru 2021-9-13
You don't need to do anything. X is already an array.
X = 'ABCDEFGHIJKLMNOPQRSTWXYZ'
X = 'ABCDEFGHIJKLMNOPQRSTWXYZ'
X(2)
ans = 'B'
X(1,2)
ans = 'B'
  5 个评论
Walter Roberson
Walter Roberson 2021-9-13
Examine of copying characters into the matrix.
X = 'ABCDEFGHIJKLMNOPQRSTWXYZ'; %row vector
X(2, [2:end,1]) = X(1,:);
X
X = 2×24 char array
'ABCDEFGHIJKLMNOPQRSTWXYZ' 'ZABCDEFGHIJKLMNOPQRSTWXY'
Walter Roberson
Walter Roberson 2021-9-13
Note that a single-quoted character literal such as 'ABCD' is considered to be a vector of characters.
clear X
X = 'ABCD';
X
X = 'ABCD'
is the same as
clear X
X(1) = 'A'; X(2) = 'B'; X(3) = 'C'; X(4) = 'D';
X
X = 'ABCD'
This is not just a notation convenience: this is how MATLAB really implements it.
X = 'ABCD'
X = 'ABCD'
double(X)
ans = 1×4
65 66 67 68
The real implementation of X = 'ABCD' is X = uint16([65 66 67 68]) %16 bits per character code followed by marking X internally as being datatype character. It is a numeric vector that is marked to display as character.
... and row vectors are the same thing as 2D arrays in which there just happens to be only 1 row. There is no difference between making X a 2D array of characters that only happens to have one row, compared to making X a row vector of character.
This is different than C. In C, you could have a declaration such as
char X[1][5]
and in C that would be different than
char X[5]
but not in MATLAB. MATLAB just has the equivalent of
struct X {
uint64 ndims;
uint64 size*;
char class[64];
uint16 flags;
(void *)data;
}
where data is the pointer to a block of consecutive memory, with there being no individual pointers into rows or columns, with the elements of the array being stored one after the other, but and the array arrangement to be interpreted according to the size field. Rearranging a 1 x 26 array into a 26 x 1 array (through transpose) just involves rewriting the size* contents from [1 26] to [26 1]

请先登录,再进行评论。

更多回答(1 个)

Awais Saeed
Awais Saeed 2021-9-13
编辑:Awais Saeed 2021-9-13
How about using cell?
X = 'ABCDEFGHIJKLMNOPQRSTWXYZ';
v = cellstr(num2cell(X))
v = 1×24 cell array
{'A'} {'B'} {'C'} {'D'} {'E'} {'F'} {'G'} {'H'} {'I'} {'J'} {'K'} {'L'} {'M'} {'N'} {'O'} {'P'} {'Q'} {'R'} {'S'} {'T'} {'W'} {'X'} {'Y'} {'Z'}
By now you use access cells as v{2}, v{4} etc.
v{2}
ans = 'B'
v{3}
ans = 'C'
To make a matrix out of it, you can reshape v as
rows = 4;
vr = reshape(v,rows,[])'
vr = 6×4 cell array
{'A'} {'B'} {'C'} {'D'} {'E'} {'F'} {'G'} {'H'} {'I'} {'J'} {'K'} {'L'} {'M'} {'N'} {'O'} {'P'} {'Q'} {'R'} {'S'} {'T'} {'W'} {'X'} {'Y'} {'Z'}
And to access elements from row, for example 2
vr{1,3}
ans = 'C'
  1 个评论
Chunru
Chunru 2021-9-13
char array is much more efficient than cell array.
X = char('A'+(0:23));
C = reshape(X, [4 6])'
C = 6×4 char array
'ABCD' 'EFGH' 'IJKL' 'MNOP' 'QRST' 'UVWX'
V = cellstr(num2cell(X));
V = reshape(V, 4, [])'
V = 6×4 cell array
{'A'} {'B'} {'C'} {'D'} {'E'} {'F'} {'G'} {'H'} {'I'} {'J'} {'K'} {'L'} {'M'} {'N'} {'O'} {'P'} {'Q'} {'R'} {'S'} {'T'} {'U'} {'V'} {'W'} {'X'}
whos % compare the storage space of C and V
Name Size Bytes Class Attributes C 6x4 48 char V 6x4 2544 cell X 1x24 48 char

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Type Identification 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by