help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: how to convert a structure array of strings into a string array?


From: Bob Odom
Subject: Re: how to convert a structure array of strings into a string array?
Date: Fri, 17 Oct 2008 15:47:55 -0700 (PDT)

How about:

octave:1> a=["eins"; "zwei"; "drei"]
a =

eins
zwei
drei


On Fri, 17 Oct 2008, Thorsten Meyer wrote:

Hi,

I would like to convert a large structure array of strings into a string
array in a nicely vectorized way.

for example:
octave:15> a={"eins", "zwei", "drei"};

should be converted into
ans =

eins
zwei
drei

=================================================
My first attempt was:


octave:16> a{:}
ans =

(,
 [1] = eins
 [2] = zwei
 [3] = drei
,)
Very nice: this generates a list of strings.

octave:17> [a{:}]
ans = einszweidrei
Here, the elements of the list are concatenated in the wrong way. :-(
=================================================

The cat function can concatenate correctly:

octave:69> cat(1, a{:})
ans =

eins
zwei
drei

However, it is incredibly slow for larger arrays and it cannot handle
strings of different length.
=================================================

This works, but I suppose it's rather slow for large arrays (also it is
not robust if the strings contains things like "):
octave:28> eval(["[", sprintf("""%s"";", a{:}), "]"])
ans =

eins
zwei
drei

=================================================

then there is the str2mat function that does the trick:
octave:14> str2mat(a{:})
ans =

eins
zwei
drei

However, looking at its source, it is only a loop over the list of input
strings. Trying with a large array
        a=cellstr(char(70+floor(20*rand(50000,100))));
the eval solution takes 5s, while the str2mat function takes 11.3s on my
computer (the cat function caused my octave to hang for that example).

Does anyone has a better idea?

thanks and regards


Thorsten Meyer

_______________________________________________
Help-octave mailing list
address@hidden
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave



reply via email to

[Prev in Thread] Current Thread [Next in Thread]