List containing tuple of detected number and it's location

I have a matrix
matrix = [["22", "na", "na"], ["na", "na", "16"], ["na", "25", "na"]]
and now I am trying to count the positions of numbers and return something like this
[{"22", 1}, {"16", 6}, {"25", 8}] .

The number “22” got position as 1 because it’s the first element ( even though it’s index is 0) and the same applies for all the elements in the expected.

I tried the following matrix |> List.flatten() |> Enum.with_index() |> Enum.filter(fn x -> !is_integer(x) end) |> Enum.to_list()
which ends up giving me
[ {"22", 0}, {"na", 1}, {"na", 2}, {"na", 3}, {"na", 4}, {"16", 5}, {"na", 6}, {"25", 7}, {"na", 8} ] . as the output , help is much appreciated.

You need to look at what data structure is output from Enum.with_index. Put an IO.inspect() between each of your pipeline steps to see what the data is.

Do you think is_integer is going to return true for anything in your input data? Hint, all your data points are strings. Look at the String and Integer module docs for something that will parse a string to an integer.

You’re also going to need to add 1 to the index at some point.

1 Like

I suggest you go with Elixir 1.14 so you can use the dbg macro which will help you a lot seeing what happens at each phase of the pipe.

3 Likes

A hint: Enum.filter/2

2 Likes

It seems that you can just modify your initial approach a little bit, like:

iex(1)> matrix = [["22", "na", "na"], ["na", "na", "16"], ["na", "25", "na"]]
iex(2)> mapping = matrix |> List.flatten() |> Enum.with_index(1) |> Enum.group_by(&elem(&1, 0), &elem(&1, 1))

%{"16" => [6], "22" => [1], "25" => '\b', "na" => [2, 3, 4, 5, 7, 9]}

and get the indexes from mapping by key name

1 Like

bro please help me to convert grid image to matrix in elixir. do you have any idea about it? from your question it seems like you have done it before. help is much appreciated.

How to convert an grid-image(supposedly the image is 3x3) into matrix

What’s exactly the format of your grid image?

A. *.png file
B. *.jpg file
C. some binary data that’s already in mem (please give the exact data format)
D. something else

What kind of matrix do you want?

A. list of lists, like

[
  [1,2,3],
  [4,5,6]
]

B. tuple of tuples, like

{
  {1,2,3},
  {4,5,6}
}

C. Nx tensor
D. something else

thank you , I was able to finish it

thank you , I was able to finish it using them also

.png file

Please share how? It helps future forum visitors.

1 Like

its an 3*3 square

like suduko image

I meant code. :smiley:

3 Likes