Clear Filters
Clear Filters

could anyone help me to solve this issue.

2 views (last 30 days)
Prabha Kumaresan
Prabha Kumaresan on 26 Mar 2018
Answered: Jan on 26 Mar 2018
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
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];
Prabha Kumaresan
Prabha Kumaresan on 26 Mar 2018
i want to separate 3 and 4, 1 and 2, 2 and 4 and so on. But how it can be implemented for n numbers.

Sign in to comment.

Answers (3)

Akira Agata
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
Prabha Kumaresan
Prabha Kumaresan on 26 Mar 2018
but when i run the code i am getting errror Undefined function or variable 'erase'. Error in line c = num2cell(erase(num2str(iwant),' '))
Walter Roberson
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.

Sign in to comment.


Walter Roberson
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
Walter Roberson
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.

Sign in to comment.


Jan
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.

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!