Using PDL http://pdl.perl.org for this particular task may (or may not) be a good idea, but, at least, worth mentioning in context of "matrices", maybe UpTide will find it beneficial to invest his time studying it. Especially because

target value could be different for each element in the future

E.g., look how concise the original program becomes (same comments in place, but code replaced):

use warnings; use strict; use PDL; use PDL::NiceSlice; #fancy matrix #set to a blank 3d test matrix my $matrix = zeroes 7,7,3; #set some values for matrix $matrix( pdl( 0..2, 4..6 ), 4, 0:2 ) .= 1; $matrix( 4, pdl( 0..2, 4..6 ), 0:2 ) .= 1; #the conditional problem child my $ind = $matrix-> whichND( $matrix == 1 ); if ( all $matrix-> indexND( $ind ) == 1 ) { print "it worked\n"; }

Well, it looks like some cheating, condition is obvious to be true.

The problem, maybe, can be seen as "there are known places in a matrix of predefined dimensions, check if they hold 'good (but different) values'". Values can be extracted from these 'known places', as shown above, and compared with pre-built vector holding 'good values'.

Of course, e.g. Athanasius's solution can be easily modified to extract values, pack them into string, compare with 'good' pre-packed string (all that without any PDL).

But it's also possible to read the question strictly verbatim:

I am hoping there would be some way to setup a 'conditional' matrix and compare them while ignoring elements that don't matter in the condition.

and write a test program:

use warnings; use strict; use PDL; use PDL::NiceSlice; use Test::PDL 'eq_pdl', -tolerance => 0.42; my $mask = zeroes 7,7,3; $mask( pdl( 0..2, 4..6 ), 4, 0:2 ) .= 1; $mask( 4, pdl( 0..2, 4..6 ), 0:2 ) .= 1; my $good_matrix = random 7,7,3; my $some_matrix; my $count; while () { $some_matrix = random 7,7,3; last if eq_pdl $good_matrix * $mask, $some_matrix * $mask; $count ++ } $PDL::doubleformat = '%.1f'; print "it worked only after $count attempts,\n", "mask was: $mask", "good matrix was: $good_matrix", "and it was not very different from: $some_matrix";

Same dimensions, same 'known places'. A mask to 'ignore elements that don't matter'. Arbitrarily chosen 'good values'. Actually, checking, how much time it takes to generate 33 random numbers in 0..1 range which are 'close enough' to 33 original numbers. Well, it looks like tolerance better than 0.4 takes forever, but only a few seconds otherwise:

it worked only after 144532 attempts, mask was: [ [ [0 0 0 0 1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.3 0.6 0.7 0.4 0.3 0.9] [0.7 0.1 0.5 0.3 0.5 0.7 0.2] [0.5 0.6 0.7 1.0 0.4 0.1 0.5] [0.4 0.8 1.0 0.6 0.4 0.3 0.7] [0.8 1.0 0.7 0.6 0.3 0.7 0.8] ] [ [0.4 0.6 0.1 0.2 0.1 0.2 0.7] [0.7 0.9 0.1 0.2 0.7 0.7 0.2] [0.9 0.6 0.0 1.0 0.6 0.7 0.8] [0.3 1.0 0.3 0.4 0.5 0.3 0.3] [0.8 0.7 0.3 0.1 0.3 0.7 0.5] [0.2 0.4 0.5 0.2 0.3 0.3 0.9] [0.9 0.7 0.4 0.1 0.2 0.9 0.8] ] [ [0.1 0.0 0.7 0.3 0.8 0.2 0.1] [0.4 0.4 0.4 0.3 0.8 0.7 0.1] [0.3 0.3 0.0 0.0 0.1 0.2 1.0] [0.8 0.1 0.7 0.7 0.4 0.3 0.3] [0.7 0.1 0.9 0.6 0.4 0.9 0.3] [0.1 0.2 0.4 0.4 0.5 0.6 0.3] [0.6 0.5 0.5 0.9 0.7 0.5 0.6] ] ]

In reply to Re: 36 Conditions in If Statement [sendhelp]] by vr
in thread 36 Conditions in If Statement [sendhelp]] by UpTide

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.