Ijlal
Ijlal

Reputation: 159

Perform index-wise matrix operation in julia

I want to perform index-wise operation on a matrix. I know that you can write a regular function and perform it on each entry of the matrix e.g.

function foo(x::Int64)
    return x * 2
end
myArray = [1 2 3; 4 5 6]
foo.(myArray)

how would I go about doing something like x * x.elementCol + x.elementrow? essentially the following code in parallel:

function goo(x::Array{Int64,2})
    for j = 1:size(x,2)  
        for i = 1:size(x,1)
            x[i,j] = (x[i,j] * j) + i    
        end
    end
    return x
end

Upvotes: 1

Views: 75

Answers (1)

DNF
DNF

Reputation: 12664

You can write:

x .= x .* indices(x, 2)' .+ indices(x, 1)

Upvotes: 4

Related Questions