Home > OS >  How to create array comprehension in Julia with two dimentions
How to create array comprehension in Julia with two dimentions

Time:01-03

i'm new to Julia and I wanted to learn how to do array comprehension. I have this lines of code.

for i in 1:m
    for j in 1:n
        arr[i, j] = i   j
    end
end

I want to do the same thing with array comprehension. I wrote this the below code, but i know this is not array comprehension. Please help me create array comprehension.

for i in 1:m, j in 1:n
    arr[i, j] = i   j

Thank you so much!

CodePudding user response:

The more Julian way of filling the array would be this (of course, I'm using an array comprehension):

arr = [i   j for i in 1:m, j in 1:n]

CodePudding user response:

your code has a little typo:

for i in 1:m, j in 1:n #Julia loops can iterate over multiple indices at once
    arr[i, j] = i   j
end

but that is not a comprehension, is just a regular for loop.

  • Related