could anyone help me to solve this issue.

2 次查看(过去 30 天)
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 个评论
Akira Agata
Akira Agata 2018-3-26
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 2018-3-26
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.

请先登录,再进行评论。

回答(3 个)

Akira Agata
Akira Agata 2018-3-26
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 个评论
Prabha Kumaresan
Prabha Kumaresan 2018-3-26
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 2018-3-26
erase() requires R2016b or later.
... It would be easier to assist you if you upgraded to a recent MATLAB version.

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2018-3-26
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 个评论
Walter Roberson
Walter Roberson 2018-3-26
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
Jan 2018-3-26
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.

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by