could anyone help me to solve this issue.
2 views (last 30 days)
Show older comments
I am getting the result in the following manner
iwant =
34 12
24 13
23 14
if i use the command line
sprintf('%d',iwant) - '0'
i am getting
ans =
3 4 2 4 2 3 1 2 1 3 1 4
But i want to have the result in the following way
(3 4 1 2,2 4 1 3, 2 4 1 4)
Could anyone help me to fix this issue.
2 Comments
Akira Agata
on 26 Mar 2018
Question for clarification.
You want to convert the following numeric 1-by-6 array into the string ('3 4 1 2,2 4 1 3, 2 4 1 4') ?
iwant = [34 12 24 13 23 14];
Answers (3)
Akira Agata
on 26 Mar 2018
Like this?
iwant = [34 12 24 13 23 14];
c = num2cell(erase(num2str(iwant),' '));
output = str2double(c);
In this case, the output is the following 1-by-12 numeric vector.
>> output
output =
3 4 1 2 2 4 1 3 2 3 1 4
2 Comments
Walter Roberson
on 26 Mar 2018
erase() requires R2016b or later.
... It would be easier to assist you if you upgraded to a recent MATLAB version.
Walter Roberson
on 26 Mar 2018
cell2mat(cellfun(@(C) C-'0', sprintfc('%d', iwant), 'uniform', 0))
This does assume that the entries all have the same number of digits (but does not assume that the number of digits is 2)
5 Comments
Stephen23
on 26 Mar 2018
@Prabha Kumaresan: Did you read this from three hours ago?:
Walter Roberson
on 26 Mar 2018
MATLAB R2015b:
>> iwant = [ 34 12
24 13
23 14]
iwant =
34 12
24 13
23 14
>> cell2mat(cellfun(@(C) C-'0', sprintfc('%d', iwant), 'uniform', 0))
ans =
3 4 1 2
2 4 1 3
2 3 1 4
Please post a complete copy of the error message you encounter when you run the code that I posted. Not someone else's code: you said that you encountered an error running my code.
Jan
on 26 Mar 2018
The best idea would be to create the numbers in the correct format directly. I guess, that you used a method I have suggested to one of your former questions, where it was not clear if you want "3, 4" or "34". If this is the case, simply omit the multiplication by [10; 1].
But to solve the current problem:
iwant = [34 12; 24 13; 23 14];
b = rem(iwant(:).', 10);
a = (iwant(:).' - b) / 10;
reshape(permute(reshape([a,b], [3,2,2]), [1,3,2]), 3,4)
[3 4 1 2; ...
2 4 1 3; ...
2 3 1 4];
Please note, that "(3 4 1 2,2 4 1 3, 2 4 1 4)" is not clear, because it is not a valid Matlab syntax.
0 Comments
See Also
Categories
Find more on Structures 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!