Convert a matrix to { }

9 次查看(过去 30 天)
Martin Park
Martin Park 2013-10-6
Can anyone help me to convert a matrix that is for instance:
0 1 2
3 4 5
6 7 8
called symbols obtained from an image to something that is in the following form symbols= {'0' '1' '2' '3' '4' '5' '6' '7' '8'}.

回答(3 个)

Jan
Jan 2013-10-6
编辑:Jan 2013-10-6
symbols = [0 1 2; ...
3 4 5; ...
6 7 8];
S = sprintf('%g*', symbols.');
S(end) = []; % Remove trailing *
C = regexp(S, '*', 'split');
Another simpler method:
C = cell(1, numel(symbols));
symbols = symbols.';
for iC = 1:numel(symbols)
C{iC} = sprintf('%g', symbols(iC));
end
I'm not convinced, that the conversion will really help to solve your problem.

Jan
Jan 2013-10-6
Or:
symbols = [0 1 2; 3 4 5; 6 7 8];
C = num2cell(char('0' + symbols.'))
  1 个评论
Jan
Jan 2013-10-6
This fails when any element of the input is outside [0, 9].

请先登录,再进行评论。


Andrei Bobrov
Andrei Bobrov 2013-10-6
编辑:Andrei Bobrov 2013-10-7
A = [0 1 2
3 4 5
6 7 8];
cellstr(sprintf('%d',A')')'
other variant:
regexp(num2str(reshape(A.',1,[])),'\d*','match')
  2 个评论
Jan
Jan 2013-10-6
编辑:Jan 2013-10-6
This fails when any element of the input is outside [0, 9].
Andrei Bobrov
Andrei Bobrov 2013-10-7
编辑:Andrei Bobrov 2013-10-7
Hi Jan, I agree with you and I suggest another option.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Symbolic Math Toolbox 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by