From: Alan Richards on
I'm using containers.map to store previous calculations and look them up later (memoization I think?). Unfortunately, you can only use char arrays or scalar numeric values with a map. To get around this, I've been calling int2str on my array, but looking at the profiler, my code is spending more than half its time in int2str.
Today I've been playing around with sprintf as a way to convert my array to string, but this seems ugly and inefficient. The code below is still considerably faster than int2str however.
Any ideas on how I could implement this better?

%as a replacement for int2str(array)
if length(array) == 1
strng = sprintf('%d', array);
elseif length(array) < 5
strng = sprintf('%d %d %d %d', array);
elseif length(array) < 9
strng = sprintf('%d %d %d %d %d %d %d %d', array);
elseif length(array) < 15
strng = sprintf('%d %d %d %d %d %d %d %d %d %d %d %d %d %d', array);
else
strng = int2str(array);
end
From: Bruno Luong on
"Alan Richards" <alarobric(a)gmail.com> wrote in message <i0aaac$li9$1(a)fred.mathworks.com>...
> I'm using containers.map to store previous calculations and look them up later (memoization I think?). Unfortunately, you can only use char arrays or scalar numeric values with a map. To get around this, I've been calling int2str on my array, but looking at the profiler, my code is spending more than half its time in int2str.
> Today I've been playing around with sprintf as a way to convert my array to string, but this seems ugly and inefficient. The code below is still considerably faster than int2str however.
> Any ideas on how I could implement this better?
>
> %as a replacement for int2str(array)
> if length(array) == 1
> strng = sprintf('%d', array);
> elseif length(array) < 5
> strng = sprintf('%d %d %d %d', array);
> elseif length(array) < 9
> strng = sprintf('%d %d %d %d %d %d %d %d', array);
> elseif length(array) < 15
> strng = sprintf('%d %d %d %d %d %d %d %d %d %d %d %d %d %d', array);
> else
> strng = int2str(array);
> end

Did you tried
sprintf('%d ', array) % ?

Bruno
From: Alan Richards on
"Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <i0b6nk$ijq$1(a)fred.mathworks.com>...
> "Alan Richards" <alarobric(a)gmail.com> wrote in message <i0aaac$li9$1(a)fred.mathworks.com>...
> > I'm using containers.map to store previous calculations and look them up later (memoization I think?). Unfortunately, you can only use char arrays or scalar numeric values with a map. To get around this, I've been calling int2str on my array, but looking at the profiler, my code is spending more than half its time in int2str.
> > Today I've been playing around with sprintf as a way to convert my array to string, but this seems ugly and inefficient. The code below is still considerably faster than int2str however.
> > Any ideas on how I could implement this better?
> >
> > %as a replacement for int2str(array)
> > if length(array) == 1
> > strng = sprintf('%d', array);
> > elseif length(array) < 5
> > strng = sprintf('%d %d %d %d', array);
> > elseif length(array) < 9
> > strng = sprintf('%d %d %d %d %d %d %d %d', array);
> > elseif length(array) < 15
> > strng = sprintf('%d %d %d %d %d %d %d %d %d %d %d %d %d %d', array);
> > else
> > strng = int2str(array);
> > end
>
> Did you tried
> sprintf('%d ', array) % ?
>
> Bruno

Well the problem with that is then you get '123' as output instead of '1 2 3'.
Then there would be no difference between [12 3] and [1 2 3] and [1 23] as inputs.
From: Bruno Luong on
"Alan Richards" <alarobric(a)gmail.com> wrote in message <i0b7pu$rf1$1(a)fred.mathworks.com>...

> >
> > Did you tried
> > sprintf('%d ', array) % ?
> >
> > Bruno
>
> Well the problem with that is then you get '123' as output instead of '1 2 3'.

No, I get '1 2 3 ', (the format is '%d' + a space ' ').

Bruno
From: Alan Richards on
"Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <i0bafl$itr$1(a)fred.mathworks.com>...
> "Alan Richards" <alarobric(a)gmail.com> wrote in message <i0b7pu$rf1$1(a)fred.mathworks.com>...
>
> > >
> > > Did you tried
> > > sprintf('%d ', array) % ?
> > >
> > > Bruno
> >
> > Well the problem with that is then you get '123' as output instead of '1 2 3'.
>
> No, I get '1 2 3 ', (the format is '%d' + a space ' ').
>
> Bruno

Oh I didn't notice the space. I wasn't aware of this format, I looked for something like this but was unable to find anything. Thanks.
Really I'm looking for a more efficient method overall to index into this container.map, rather than having to convert to strings in the first place. Maybe in the future Matlab will support vectors as keys.