how to convert vector char to matrix char ?
Show older comments
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 Comment
Stephen23
on 13 Sep 2021
Using a cell array for this is pointlessly complex and inefficient.
Using a character array, as Chunru shows, is simpler and more efficient.
Accepted Answer
More Answers (1)
Awais Saeed
on 13 Sep 2021
Edited: Awais Saeed
on 13 Sep 2021
How about using cell?
X = 'ABCDEFGHIJKLMNOPQRSTWXYZ';
v = cellstr(num2cell(X))
By now you use access cells as v{2}, v{4} etc.
v{2}
v{3}
To make a matrix out of it, you can reshape v as
rows = 4;
vr = reshape(v,rows,[])'
And to access elements from row, for example 2
vr{1,3}
1 Comment
char array is much more efficient than cell array.
X = char('A'+(0:23));
C = reshape(X, [4 6])'
V = cellstr(num2cell(X));
V = reshape(V, 4, [])'
whos % compare the storage space of C and V
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!