Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

intersection of scalar with 1D array and 2D array Matlab=>Perl

by f77coder (Beadle)
on Aug 30, 2014 at 02:58 UTC ( [id://1099055]=perlquestion: print w/replies, xml ) Need Help??

f77coder has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I'm converting some Matlab code into perl. Here's the Matlab code.

while ~feof(fileIDS); m1=fgetl(fileIDS); [a,b]=intersect(dataD{1},{deblank(m1)}); if (~isempty( a)) data=dataD{2}(b); elseif(~isempty(intersect(dataT{1},{deblank(m1)})) ) data=1.0; else data=0.0; end; end;

dataD is a 2D array, dataT is a 1D array

m is a scalar value read from a file

a=returns index if there is one and null if it doesn't and b=value at index a

Basically what this does is the following

intersection (2D data,scalar) if intersection exists not null a=index of location data= value of location at b elseif intersection (1D data,scalar) data=1.0 else data=0.0 endif

I'm reading through this http://perldoc.perl.org/perlfaq4.html#How-do-I-test-whether-two-arrays-or-hashes-are-equal%3f but this assumes that the arrays are of equal dimensions (1D).

I'd like to know what is the simplest and fastest way?

use 5.12.0; use strict; use warnings; use diagnostics; use List::MoreUtils; open my $TEST, '<', $f_Test or die "Could not open=> $f_Test : $!"; my $data; while (my $scalar=<$TEST>) { chomp($scalar); [my $a, my $b]=firstidx { $_ == $scalar } @arrayPFA; if(!exists $a) { $data=$arrayPFA[$b]; } elsif(if any { ! defined($scalar) } @arrayT;) { $data=1.0; } else { $data=0.0; }; }; close $TEST

Thanks for any help.

Replies are listed 'Best First'.
Re: intersection of scalar with 1D array and 2D array Matlab=>Perl
by zentara (Archbishop) on Aug 30, 2014 at 08:44 UTC
    I might add that PDL might be the most flexible solution. It's piddles can handle matrix manipulation fast and with some cool tricks too, that you might never expect. It seems to me you are looking to solve the problem of how to find the intersection of a vector and a planar surface. If I were you, I would ask this on the very friendly and helpful PDL maillist. The geniuses there ( not including me :-) ) have learned the tricks of piddle fu, and may just know the perfect piddle you need to be the fastest and most efficient. Which is the name of the game of course. :-)

    PS: PDL was designed in Fortran, which I assume, from your name, interests you f77coder


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

      Thanks, this looks very promising. And yes I do prefer direct matrix manipulation versus associative arrays.

      Where did the OSX binary go? It no longer on Sourceforge.

      http://sourceforge.net/projects/pdl/files/

      PDL is an interesting build on 10.8. Some difficult dependencies to resolve and it only likes gfortran-4.10 which I had to build. Sometimes it finds the compiler F77 for dependencies sometimes it doesn't.

        My recommendation for current MacOS: use Homebrew to install Perl, then install local::lib, then use Homebrew to install gfortran and other optional dependencies to get more of the optional stuff in PDL working, then use `cpanm PDL`. Ideally we'd have an up-to-date PDL packaged for MacOS, but that isn't currently the case.
Re: intersection of scalar with 1D array and 2D array Matlab=>Perl
by NetWallah (Canon) on Aug 30, 2014 at 04:07 UTC
    A few modules that are likely to do the job well: Other than that, some perlish syntax correction (NO logic correction) to your code:
    while ( defined (my $scalar=<$TEST>) ) # This will accommodate the cas +e when $seclar is Zero { chomp($scalar); my ( $a, $b ) =firstidx { $_ == $scalar } @arrayPFA; # $a and $b ar +e NOT good names - they have special meanings in perl if(!defined $a) { $data=$arrayPFA[$b]; } elsif(if any { ! defined($scalar) } @arrayT;) # No idea what is inte +nded here... { $data=1.0; } else { $data=0.0; }; };

            "You're only given one little spark of madness. You mustn't lose it."         - Robin Williams

      This will accommodate the case when $seclar is Zero
      Not needed:
      $ perl -MO=Deparse -e 'while (my $scalar = <$TEST>) {1}' while (defined(my $scalar = <$TEST>)) { do { '???' }; } -e syntax OK

      Relevant: To Kill a Meme: while(defined($line = <>))

      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        Correct, scary, and certainly not obvious.

        Thanks for the education.

        More discussion on this issue is at stack overflow.

                "You're only given one little spark of madness. You mustn't lose it."         - Robin Williams

      Thanks for suggestion.

      I tried List::Compare and most of the flavors but they all bomb because I'm comparing a scalar to an array. All want 1D arrays to compare. I'll have a look at the others.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-04-26 01:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found