replace numbers with letters in a vector or matrix
Show older comments
optEnergyCost=[42 36 28.5 26 39.7 25 57047]
optEnergyCostString=sprintf('%g',optEnergyCost)
optEnergyCostString=strrep(optEnergyCostString,'42','Route_1');
optEnergyCostString=strrep(optEnergyCostString,'36','Route_1');
optEnergyCostString=strrep(optEnergyCostString,'28.5','Route_2');
optEnergyCostString=strrep(optEnergyCostString,'26','Route_2');
optEnergyCostString=strrep(optEnergyCostString,'39.7','Route_1');
optEnergyCostString=strrep(optEnergyCostString,'25','Route_1');
but results are
optEnergyCostString =
423628.52639.72557046.5
How do I correct this?
Accepted Answer
More Answers (4)
Image Analyst
on 15 Dec 2016
You need to convert it to a string, then use strrep():
m=[2 2.8 2.5 2 2.5 2.8 2.5 37243]
mString = sprintf('%g ', m)
mString = strrep(mString, '2 ', 'worker1 ');
mString = strrep(mString, '2.8 ', 'worker2 ');
mString = strrep(mString, '2.5 ', 'worker3 ')
You see in the command window:
mString =
2 2.8 2.5 2 2.5 2.8 2.5 37243
mString =
worker1 worker2 worker3 worker1 worker3 worker2 worker3 37243
the cyclist
on 15 Dec 2016
The reason the code is not working is that the example was substituting individual characters, not longer strings.
This will work, if you can use a cell array to store the values.
m = [2 2.8 2.5 2 2.5 2.8 2.5 37243];
res = cell(size(m));
[unique_m, ~, idxFromUniqueBackToAll] = unique(m);
for i = 1:numel(unique_m)
res(idxFromUniqueBackToAll==i) = {['worker',sprintf('%d',i)]};
end
There are admittedly a lot of little tricks in there to make things work, so it might take you some time to fully understand what is happening in this code.
Image Analyst
on 16 Dec 2016
summiya:
With your new example, you didn't quite follow what I gave you in my answer before. You forgot to put spaces after the %g, after the numbers, and after the words that you want to replace the numbers with. Try doing that, like this:
optEnergyCost=[42 36 28.5 26 39.7 25 57047]
optEnergyCostString=sprintf('%g ',optEnergyCost)
optEnergyCostString=strrep(optEnergyCostString,'42 ','Route_1 ');
optEnergyCostString=strrep(optEnergyCostString,'36 ','Route_1 ');
optEnergyCostString=strrep(optEnergyCostString,'28.5 ','Route_2 ');
optEnergyCostString=strrep(optEnergyCostString,'26 ','Route_2 ');
optEnergyCostString=strrep(optEnergyCostString,'39.7 ','Route_1 ');
optEnergyCostString=strrep(optEnergyCostString,'25 ','Route_1 ');
The result is now:
optEnergyCostString =
Route_1 Route_1 Route_2 Route_2 Route_1 Route_1 57047
4 Comments
summyia qamar
on 17 Dec 2016
summyia qamar
on 17 Dec 2016
Image Analyst
on 17 Dec 2016
You should also learn that you can :
- use the debugger and you can hover over the variable and a popup message will show you the value of the variable, or
- you can type the value onto the command line to report the value to the command window, or
- you can look at the variable value in the workspace panel, or
- you can double click the variable name in the workspace panel to view the variable in the variable editor window.
So there are lots of ways you can see what the variable is other than just leaving off a semicolon to echo it to the command window.
summyia qamar
on 19 Dec 2016
Andrei Bobrov
on 15 Dec 2016
m=[2 2.8 2.5 2 2.5 2.8 2.5 37243]';
[~,~,c] = unique(m,'stable');
str = arrayfun(@(x)sprintf('worker%d',x),(1:4)','un',0);
out = num2cell(m);
t = m < 4;
out(t) = str(c(t));
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!