I can't figure out how to take a matrix and count the amount of the alphanumerical values for each row. I will only be taking in matrices with the values I'm counting. For example, if I got:
ABA455
7L9O36G
DZLFPEI
I would get something like A:2 B:1 4:1 5:2 for the first row and each row would be counted independently. I would most like to understand the operators used if you could please explain them too. Thank you.
The following should work in any mainstream APL implementation.
Let's start with a simple vector of characters:
We can find the unique characters by filtering to keep only elements that have the same index as the first occurrence of themselves:
Now we compare the unique elements to every element:
Summing this table horizontally gives us the occurrence count for each unique element:
Now we just need to pair up the unique elements with their respective counts:
Let's put that into a utility function:
Now we need to apply this function on each row of the matrix. We start by splitting the matrix into a vector of vectors:
Then we apply the utility function to each vector:
Try it online!
If you're using Dyalog APL, then the Key operator (
⌸
) is very much what you need:It takes a single operand and calls it once per unique value, with the specific value as left argument and the list of occurrence indices as right argument. However, we're not interested in the actual occurrences, only in their counts:
Now we simply have to apply this function on each row. We can do this by splitting the matrix and applying the function with Each:
Try it online!