fabmeyer

fabmeyer

Help with updating every single element in tensor in a loop

Beginner question here:
I want to process all elements in a given tensor in a loop (or one go). As far as i understand, in Elixir you cannot access variables from outer scopes. So the following code does nothing:

  def update_all_cells(world) do
    shape = Nx.shape(world)
    rows = elem(shape, 0)
    columns = elem(shape, 1)
    # we need a temp_world because we cannot update the world
    # as we need it to calculate the next state
    temp_world = fn (world, rows, columns) ->
      temp_world = Nx.tensor(world, names: [:x, :y, :cell_info], type: {:s, 64})
      Enum.each(0..rows-1, fn x ->
        Enum.each(0..columns-1, fn y ->
          updated_cell = update_cell(world, x, y)
          temp_world = update_world(world, x, y, updated_cell)
        end)
      end)
      temp_world
    end
    temp_world.(world, rows, columns)
  end

I understand that I need to use something like Enum.map, however that only works for enumerables and so does not work for Nx.tensors. I saw that there is Nx.map…
I can call something like Nx.map(world[cell_info: 0..-1//1], fn x -> some_function(x) end) however I still need to define my variables and update them somehow. I am kind of lost here :smiley:
Can somebody help me?

First Post!

Hermanverschooten

Hermanverschooten

I have never used or even looked at Nx, but I see in the docs that map is not preferred and while is, that seems to allow you to pass in and update variables.

Where Next?

Popular in Questions Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

We're in Beta

About us Mission Statement