3

Given the matrix:- A = [0 1 2 3 4 5];

I want to convert it into a string cell array like this: A = {'0' '1' '2' '3' '4' '5'};

I am able to do this using:

A = [0 1 2 3 4 5];

for i=1:6
    A1{i}= num2str(A(i));
end
A1

I want to do this in a simpler way and without a loop.

Sardar Usama
  • 19,536
  • 9
  • 36
  • 58

3 Answers3

5

Another one line method with num2str and strsplit:

A1 = strsplit(num2str(A))
obchardon
  • 10,614
  • 1
  • 17
  • 33
  • +1 nice and fast solution, but for compatibility and speed reasons I would suggest to use `regexp` `A1 = regexp(num2str(A), '\s+', 'split');` In my tests `regexp` is seven times faster than `strsplit` – serial Jul 04 '16 at 12:14
3

you may use arrayfun in combination with an anonymous function:

B = arrayfun(@(x) {num2str(x)}, A);

cellfun is a little bit faster and works also fine:

B = cellfun(@num2str, num2cell(A), 'uni', 0);

fastest solution is an improved version of this solution (credits to obchardon)

B = regexp(num2str(A), '\s+', 'split');
Community
  • 1
  • 1
serial
  • 447
  • 4
  • 12
  • 1
    `arrayfun` is just a wrapper for the loop, so it's basically identical to the OP. – Dan Jul 04 '16 at 11:39
  • @Dan Well not exactly. This will actually give one number per cell and the OP is will create a string with one char in each cell. I actually think this is closer to what the question is about, since this is the only answer producing equivalent code. – patrik Jul 04 '16 at 11:49
  • 1
    @Patrik I don't see a difference between this and the OP loop, even for multiple digit numbers... – Dan Jul 04 '16 at 12:08
  • 1
    I agree with @Dan for me there is no "computational" difference, this method remains slow. – obchardon Jul 04 '16 at 12:24
0

The solutions below are ordered roughly from fastest to slowest. Note how the solutions fall into three order-of-magnitude performance classes.

This is using precompiled Octave 4.2.2 from MacPorts on an iMac; octave @4.2.2_1+accelerate+app+docs+fltk+gfortran+graphicsmagick+qt5+sound.

Elapsed time is 0.00452113 seconds.
Elapsed time is 0.0121579 seconds.
Elapsed time is 0.0185781 seconds.
Elapsed time is 0.0243361 seconds.
Elapsed time is 0.025944 seconds.
Elapsed time is 2.42572 seconds.
Elapsed time is 2.4809 seconds.
Elapsed time is 2.48733 seconds.
Elapsed time is 2.49299 seconds.

Takeaways: Prefer sprintf over any other to-string conversions, and ostrsplit over strsplit.

clear all
A=rand(1,2000);
#A=1:2000;

tic
A4=ostrsplit(sprintf("%g ",A), " ", true);
toc;tic

A9=ostrsplit(num2str(A), " ", true);
toc;tic

A8=regexp(num2str(A), '\s+', 'split');
toc;tic

A3S=num2str(A');
A3=mat2cell(A3S,ones(1,size(A3S,1)))';
A3=strtrim(A3);
toc;tic

A5=strsplit(num2str(A));
toc;tic

A7=cellfun(@num2str, num2cell(A), 'uni', 0);
toc;tic

A6=arrayfun(@(x) {num2str(x)}, A);
toc;tic

A2=cell(size(A));
for i = 1:numel(A)
  A2{i} = num2str(A(i));
endfor
toc;tic

for i = 1:numel(A)
  A1{i} = num2str(A(i));
endfor
toc;tic
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
  • The question is not tagged Octave. So some of the solutions are irrelevant, others are already posted. Only one of them is new. It would have been better to acknowledge the already posted solutions that you have included in yours. `'` is not transpose. – Sardar Usama May 21 '18 at 08:50