Exercism Matrix Task 5

Hi again!

I need your help again, please:

I solved the five tasks now.
Two of them first traditionally, by looping over the Matrix’s elements and then tried to do it with the comfortable functions Julia provides.

and now: I’m stuck with Task 5, render
the hints say:
solving it with eachrow
the question is: how can I loop over it, as I cannot access a single element of each row only the whole row itself.

maybe I got the hints wrong?

the code, so far:

function render(E)
    E_r=map(x -> x,E)
#=Using eachrow() to loop may work here.
The function join() can also help to make things more concise.=#
    a_E_rows = eachrow(E)
    join(a_E_rows, "\n")

thanks for your answers which I'm looking forward to read!

Nisang

And, a question:

with colpixelcount traditionally, by looping over a copy of the Matrix’s element and, with broadcasting,
first with

#    a_cpc_E = sum(eachrow(E)) # 18 Element-Vector

what didn’t work (the compiler gives an error, as below, but the output matrix looks fine), why?

then with

    a_cpc_E = sum(E;dims=1) #1×18 Matrix

what works,

the code:

function colpixelcount(E)
    # with Broadcasting
    E_cp=map(x -> x,E)
    # count the number of dots in every column(NODC), save it in vector of column
    # use eachrow(), eachcol() or eachslice() to create a view to loop on it
#=    a_cpc_E = sum(eachrow(E)) # 18 Element-Vector
    broadcast(*, E_cp, a_cpc_E) ERROR with 18 Element-Vector: DimensionMismatch: arrays could not be broadcast to a common size: a has axes Base.OneTo(11) and b has axes Base.OneTo(18)
    broadcast(*, E_cp, [a_cpc_E]) # this works, but why? and, uses comprehension, too=#

    a_cpc_E = sum(E;dims=1) #1×18 Matrix
    # write NODC to every element which is 1 
    # 1*8 = 8, 0*8 = 0!
    broadcast(*, E_cp, a_cpc_E)
end

Hi Nisang!

Good job on getting through the first four tasks!

Your code join(eachrow(E_xo), "\n") is not too far off from a solution (assuming you have converted the 0’s and 1’s to spaces and X’s, respectively), but you’re missing a step. Remember that we are dealing with a matrix, so there may be multiple loops (i.e. joins) involved.

One thing you can consider is, what is the difference between the following cases?

join([[1,2,3]], "\n")

join([1,2,3], "\n")

And compare these examples with what your code is producing vs what is expected.

A few questions to help point you in the right direction:

  • What are the dimensions of all the items in question?
  • Is a 1x18 Matrix the same as an 18 Element-Vector?
  • What does wrapping a Vector in another Vector effectively do to the dimensions?