How to add a string to all member in matrix?
1 view (last 30 days)
Show older comments
Example: I have:
a = [1 2 3; 4 5 6; 7 8 9]
I want have:
b = [s1 s2 s3; s4 s5 s6; s7 s8 s9]
4 Comments
Accepted Answer
the cyclist
on 9 Jun 2015
Edited: the cyclist
on 9 Jun 2015
I have the same questions that James Tursa asked, but here is my best guess as to what you want:
b = cellfun(@(x)['s',num2str(x)],num2cell(a),'UniformOutput',false)
More Answers (1)
James Tursa
on 9 Jun 2015
Edited: James Tursa
on 9 Jun 2015
For a symbolic matrix output, e.g.,
n = numel(a);
s = '[';
for k=1:n
e = ['s' num2str(a(k))];
eval(['syms ' e]);
s = [s ' ' e];
end
s = [s ']'];
b = reshape(eval(s),size(a));
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!