could anyone help me to solve this issue.

조회 수: 1 (최근 30일)
Prabha Kumaresan
Prabha Kumaresan 2018년 3월 26일
답변: Jan 2018년 3월 26일
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에서 Entering Commands에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by