bug in char, str2mat and strvcat behaviour?
Thorsten Meyer
thorsten.meyier at gmx.de
Tue Nov 11 14:46:49 CST 2008
David Bateman wrote:
> To fix the behavior of char in Octave the convert_to_str method of the
> octave_value class needs to be changed. However, that will have other
> implications, particularly for mixed type operation. Like what does
> matlab return for
>
> char(50,50,[],50)
> char([50;50;[],50])
> [50;"2";"";"2"]
>
An idea:
matlab obviously shows both kinds of behaviour in different circumstances. So from that point of
view there does not seem to be a clear preference or default behaviour in general to either remove
empty strings or keep them: it removes them in matrix definition and strvcat, it keeps them in char,
str2mat and strangely also whithin cell arguments to strvcat.
Why don't we just rename the existing char function implemented in src/strfns to strvcat and use it
to reimplement char (and str2mat) as an m-file like this:
function [chararray] = my_char (varargin);
chararray = char(varargin);
endfunction
This way we get a much faster strvcat which behaves almost like the existing one except that it is
even more permissive with respect to the possible inputs:
At the moment this is possible:
octave:35> char(124, {"thorsten", "ist", {"doof", "bla", "bli"}})
ans =
|
thorsten
ist
doof
bla
bli
But this gives:
octave:36> strvcat(124, {"thorsten", "ist", {"doof", "bla", "bli"}})
error: A(I,J,...) = X: dimensions mismatch
error: called from:
error: /home/thorsten/hg/octave/scripts/strings/strvcat.m at line 98, column 17
The above function my_char gives:
octave:36> my_char(50,50,[],50)
ans =
2
2
2
octave:38> my_char("orange", "green", "", {"red", [100, 101, 102], {"bla", [], ["thorsten"; "ist";
"doof"]}})
ans =
orange
green
red
def
bla
thorsten
ist
doof
octave:39> my_char(50,50,[],50)
ans =
2
2
2
octave:40> my_char([50;50;[],50])
ans =
2
2
2
So it would be nicely compatible to matlab as far as I can see.
Also, here is a timing test for the three functions char, strvcat and my_char on my machine:
octave:49> x=char(floor(100+25*rand(100000, 100)));
octave:50> b=cellstr(x);
octave:51> tic;char(b); toc
Elapsed time is 4.8607 seconds.
octave:52> tic;my_char(b); toc
Elapsed time is 5.7303 seconds.
octave:53> tic;strvcat(b); toc
Elapsed time is 242.56 seconds.
octave:59> tic;char(b{:}); toc
Elapsed time is 4.9695 seconds.
octave:60> tic;my_char(b{:}); toc
Elapsed time is 5.2355 seconds.
octave:61> tic;strvcat(b{:}); toc
Elapsed time is 43.941 seconds.
Only one open question for me: is the char function used somewhere in the c++ sources? And how would
I look for such a use? (grep for char gives hundrets of hits because of the char keyword in c++).
If you agree, I will provide a patch for this.
regards
Thorsten
More information about the Bug-octave
mailing list