Transfer from Decimal to Percentage?

Hi guys,
I have a Matrix A =
[1 2 3
4 5 6
7 8 9]
How I can get Matrix B =
[100% 200% 300%
400% 500% 600%
700% 800% 900%]?
Sprintf might work but I don't know how to use it. Thanks in advance.

2 Comments

You cannot get it as a numeric matrix, as numeric matrices cannot contain '%' characters.
Then how can I transfer it to string or cell array? As long as there follows '%'.
I was wondering if there is a function to do this...

Sign in to comment.

 Accepted Answer

thisfmt = [ repmat(' %.3f%%', 1, size(A,2)) '\n'];
B = sprintf( thisfmt, A.' );
B(1) = '[';
B(end) = ']';
This creates the output as a single string.

8 Comments

Thanks first. I was wondering if there is a way to create a 3 by 3 matrix instead of a scalar?
And the result is 100 times smaller.
100 * A.' to get the 100 times bigger.
Anyhow:
B = arrayfun(@(V) sprintf('%f%%', V), 100*A, 'Uniform', 0);
This will not, however, produce the result you had asked for: it will instead produce
B = { '100%' '200%' '300%'; '400%' '500%' '600%'; '700%' '800%' '900%'};
Did he really want the square brackets (and question mark) in the output, or was he just using that because he was trying to indicate an array of some type?
The brackets are not necessary. I just want to find a quick way to transfer a particular matrix out of several matrix into percent format.
So does the solution offered by Walter (a cell array of strings) work for you? Is so, mark this as "Accepted."
for what it's worth, if you want it in a cell array:
A=reshape(1:9,3,3)';
cellfun(@(x) sprintf('%0.1f%%',x*100),num2cell(A),'UniformOutput',false)
Thanks Tom! That's perfect!
Tom this works great!! Thanks

Sign in to comment.

More Answers (0)

Products

Asked:

on 25 Jun 2012

Commented:

on 14 Sep 2016

Community Treasure Hunt

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

Start Hunting!