Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: how to get average of matrices' elements?

by toolic (Bishop)
on Nov 30, 2016 at 18:58 UTC ( #1176963=note: print w/replies, xml ) Need Help??


in reply to how to get average of matrices' elements?

The first time through your nested for loops, @m_avrg is uninitialized. You can uninitialized with:
my @m_avrg; for my $i ( 0 .. 2 ) { $m_avrg[$i] = [ map { $_ = 0 } 1 .. 3 ]; # $m_avrg[$i] = [ map { $_ => 0 } 1 .. 3 ]; # WRONG! }

UPDATE: I copy'n'pasted the wrong code originally. Yes, Anon's x operator is better.

See also: Basic debugging checklist

Use 4 single spaces for each level of indentation.

Replies are listed 'Best First'.
Re^2: how to get average of matrices' elements?
by Anonymous Monk on Nov 30, 2016 at 19:07 UTC
    That map is wrong, we're not building a hash. Just use $m_avrg[$i] = [ (0) x 3 ];
      That map is wrong, we're not building a hash.
      Please try running my code before you make such a claim. My code creates an array-of-arrays data structure with all elements initialized to 0.

      UPDATE: see my updated code.

        I did run your code. Here's the output from Data::Dump:
        [[1, 0, 2, 0, 3, 0], [1, 0, 2, 0, 3, 0], [1, 0, 2, 0, 3, 0]]
Re^2: how to get average of matrices' elements?
by Anonymous Monk on Nov 30, 2016 at 19:10 UTC
    Also, another way to avoid the "uninitialized variable" warning is to use the += operator.
    instead of this: $m_avrg[$a][$b] = ($m_avrg[$a][$b] + $list[$a][$b]); use this: $m_avrg[$a][$b] += $list[$a][$b];

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1176963]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2022-01-20 22:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (57 votes). Check out past polls.

    Notices?