Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Topic: speeding up assigning values to a matrix
Replies: 7   Last Post: Apr 8, 2013 3:52 AM

 Messages: [ Previous | Next ]
 dpb Posts: 9,850 Registered: 6/7/07
Re: speeding up assigning values to a matrix
Posted: Apr 5, 2013 3:23 PM

On 4/5/2013 11:32 AM, Luca wrote:
...

> Now... I tried a lot of different operations and I discovered that it is
> extremely slow to do
> (casting or not num to uint8 changes less than 0.1%)
>
> I found it insted to be very fast, acceptably for my scope, to do
>
> the difference between the (1) and (2) is 300ms for the first one and
> 15ms for the second one!!!
> A factor 20.
> Very curious indeed.
>

I see you have found what I was going to suggest; namely that doing
computations in non-natural word lengths can be very time-consuming
owing to the behind-the-scenes casts and addressing problems of not
pulling out and working on full words.

I'd expect if you did the operation entirely on doubles and only cast
the result for it to be slightly better.

> (still wondering wether I can use logical indexing to do something like:

No. There's a mismatch in dimensions of the logical addressing array
and the target that ML doesn't handle. It seems like it should be
possible to do so, but the syntax isn't implemented.

What happens is shown by the following much smaller example of your case...

>> m=randi(7,5,5,2)-1;
>> ix=m==3

ix(:,:,1) =
0 1 0 0 0
0 0 0 1 0
0 0 1 0 0
0 0 0 0 0
0 0 0 0 0
ix(:,:,2) =
0 0 0 0 0
0 0 0 0 1
0 0 0 1 1
0 1 0 0 0
1 0 1 0 0
>> M(ix)
ans =
0
0
0
0
0
0
0
0
0
>>

As you see, when you try to index into M w/ ix, the logical T elements
are returned as a vector. The syntax of keeping ix as the two planes
and then adding another dimension just doesn't work--ML doesn't know how
to do that.

Your only way would be to turn the indices locations in ix above into
the linear addresses in the 4D array based on the sizes and the
column-major storage order expression.

--

Date Subject Author
4/5/13 Luca
4/5/13 kinor
4/5/13 Luca
4/5/13 kinor
4/5/13 Luca
4/5/13 dpb
4/5/13 Luca
4/8/13 kinor