10-13-2009 11:31 AM
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
Solved! Go to Solution.
10-13-2009 12:21 PM
10-13-2009 04:15 PM
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