Merge elements of a row into a single element array

20 次查看(过去 30 天)
Hi,
I want to merge each row of CHAR type matrix
BB=[C I M G 2 8 3 0; C I M G 2 8 3 2; C I M G 2 8 3 3; C I M G 2 8 3 4]
as
BBB=[CIMG2830; CIMG2832; CIMG2833; CIMG2834]
or in other words I want to merge each row into one single element, not necessarily all elements are numbers.
Any ideas?

采纳的回答

Walter Roberson
Walter Roberson 2016-9-11
BB=['C' 'I' 'M' 'G' '2' '8' '3' '0'; 'C' 'I' 'M' 'G' '2' '8' '3' '2'; 'C' 'I' 'M' 'G' '2' '8' '3' '3'; 'C' 'I' 'M' 'G' '2' '8' '3' '4']
is already completely equivalent to
BBB = ['CIMG2830'; 'CIMG2832'; 'CIMG2833'; 'CIMG2834']
Are you sure you are starting with a char matrix? And not, perhaps, a cell array whose elements are each single char ? If what you have is
BB={'C' 'I' 'M' 'G' '2' '8' '3' '0'; 'C' 'I' 'M' 'G' '2' '8' '3' '2'; 'C' 'I' 'M' 'G' '2' '8' '3' '3'; 'C' 'I' 'M' 'G' '2' '8' '3' '4'}
then
BBB = reshape( horzcat(BB{:}), size(BB,1), [])
  3 个评论
Walter Roberson
Walter Roberson 2016-9-11
编辑:Walter Roberson 2016-9-11
Do not use cell2mat() for that purpose. Instead, use
char(aa)
as that will convert the cell array of strings into a row-based array of char.
However, you would seldom need that. You would typically instead use something like,
filename = aa{k};
fullname = fullfile(folder, filename);
JohnDylon
JohnDylon 2016-9-11
That worked great for me. Thank you for pointing a torch on this.

请先登录,再进行评论。

更多回答(0 个)

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by