Take a vector of numbers, and convert them to a string and store in a single cell?
1 view (last 30 days)
Show older comments
Say I have:
S = [1 2 3]
I want this stored in a matrix which already has stuff in it:
A =
8 1 6
3 5 7
4 9 2
And end up with something like this:
A =
8 1 6 123
3 5 7 123
4 9 2 123
I've tried playing with num2str and a few cat commands, but they all give me error. May I mention this is the first time I've been dealing with strings (which according to me, the only way to have various individual numbers stored as a "list" is to convert them to strings).
0 Comments
Answers (2)
dpb
on 20 Oct 2015
If you leave as string, can't put in the numeric array; would have to use a cell array instead. OTOH, if you convert to the string and then back to numeric, you can "have your cake and eat it too"...
A(:,end+1)=num2str(sprintf('%d',s));
for your specific example above.
4 Comments
Star Strider
on 20 Oct 2015
Another approach:
A = [ 8 1 6
3 5 7
4 9 2];
S = [1 2 3];
A = [A repmat(S*[100; 10; 1], size(A,1), 1)]
3 Comments
Star Strider
on 20 Oct 2015
I’m just Answering the Question posted. My code can be made as robust as necessary.
See Also
Categories
Find more on Characters and Strings 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!