Thanks, guys, for so many responses.  I will try the squeeze() trick.  

Unfortunately, I can't write my array as n x n x 3.  I'm using it elsewhere 
where I need the short dimension to be unit stride. That other section is 
more performance critical than this part. I would rather it be amenable to 
broadcasting, but I'm guessing that copying it to alter the strides is 
costlier than writing it in a for loop.


On Wednesday, August 27, 2014 12:03:44 PM UTC-5, Ethan Anderes wrote:
>
> Good catch Kevin. Broadcasting works. Cheers
>
> julia> function f1(n)
>           A = rand(n,n,3) 
>           S = rand(n,n)
>           A ./ S 
>        end
> f1 (generic function with 1 method)
>
> julia> function f2(n)
>            A = rand(n,n,3); 
>            S = rand(n,n); 
>            for k = 1:size(A,3) 
>                A[:,:,k] = A[:,:,k] ./ S; 
>            end 
>            return A 
>        end
> f2 (generic function with 1 method)
>
> julia> begin
>            srand(1)
>            x = f1(10)
>            srand(1)
>            y = f2(10)
>            x == y
>        end
> true
>
> On Wednesday, August 27, 2014 9:57:40 AM UTC-7, Kevin Squire wrote:
>
> Good thought, Ethan!  In fact, if you do it that way, broadcasting should 
>> work--just divide by S.  (I'm not at a computer, so please test.) 
>>
>> Cheers,
>>    Kevin
>>
>> On Wednesday, August 27, 2014, Ethan Anderes <[email protected]> wrote:
>>
>>> You could also re-arrange the indices and move the for loop to the last 
>>> index
>>>
>>> function f(n)
>>>     A = rand(n, n, 3) 
>>>     S = rand(n, n) 
>>>     for k = 1:size(A, 3) 
>>>         A[:,:,k] = A[:,:,k] ./ S 
>>>     end 
>>>     return A 
>>> end
>>>
>>> This works since A[:,:,k] is a 2-d array but A[k,:,:] is a 3-d array.
>>> ​
>>>
>>  ​
>

Reply via email to