LabVIEW MathScript RT Module

cancel
Showing results for 
Search instead for 
Did you mean: 

boolean index of array

Solved!
Go to solution

In Matlab often use a boolean array to access specific values in an array.

This is a good way to reduce itterative loops and improve code speed.

 

I am trying to find a way to do this in a Mathscript node as otherwise the code takes ages to run.

 

Common code in Matlab and Mathscript: 

a=[1,2,3,4,5]; %normally this is a much larger array

b=[1,0,1,0,1]; %I want this to be boolean, but by default it is double

c=b>0;  %change b into boolean

d=[9,9,9,9,9]; 

 

The Matlab code is:

d(c)=a(c);

d

 

>>d=[1 9 3 9 5]

 

If I run the above in Mathscript I get the following error:

 

 

In Mathscript, to get the same "d" result I currently do:

for i=1:length(d) 

   if c(i)==1

      d(i)=a(i);

   end

end

d

 

>>d=[1 9 3 9 5]

 

A slightly faster version is:

for i=1:length(d)

   j=1;

   if c(i)==1

     e(j)=c(i);

      j=j+1;

   end

end

d(e)=a(e);

d

 

>>d=[1 9 3 9 5]

 

 

Any ideas?

 

Thanks

0 Kudos
Message 1 of 3
(8,780 Views)
Solution
Accepted by topic author stubon
Hello,

The code you posted works fine in the MathScript node in LabVIEW 2009 as well as in the MathScript Window and the MathScript node in LabVIEW 8.6.  As such, I will assume you are getting an error in the MathScript Window in LabVIEW 2009.  We have already identified this issue and currently have filed bug report 151999 for it.  The error message you are seeing did not come through in your post, so I assume it is -90026: "The indexes for a matrix indexing operation must be real, positive integers."  The problem is that we are not treating the "c" indexing variable as a boolean.  As such, this reports an error when it tries to use index 0 instead of a false.  We are working on a fix for a future release.

As a workaround in the MathScript Window, you can use the find command in lieu of any boolean indexing.  It should operate quicker than the loop implementation you reference.  Instead of
d(c) = a(c);
use
d(find(c)) = a(find(c));

Grant M.
Staff Software Engineer | LabVIEW MathScript | National Instruments
Message 2 of 3
(8,778 Views)

Thanks for the quick reply Grant.

Indeed you were correct with the error. I forgot to paste it in.

You were also correct that I was testing it in the Mathscript Window which produced the error.

I now have the code working in the main vi node.

 

thanks 

0 Kudos
Message 3 of 3
(8,766 Views)