LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

max & min of 2D array columns

Solved!
Go to solution

Is it possible to find the max/min value for each row/column of a 2D array without a loop?

0 Kudos
Message 1 of 14
(7,989 Views)

I recommend using the FOR loop with autoindexing tunnels so that you can do the comparison for each row/column.  Your alternative is to use Array Index to get the rows/columns you want and then do the comparison.  Not very scalable and way longer to program.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 2 of 14
(7,977 Views)

The goal is to not use a loop. Is this possible in LabVIEW? For example, in other programming languages, for a 2D array 'A',  one could write max(A) and it returns the max of each coumn.

0 Kudos
Message 3 of 14
(7,968 Views)

Hi jmountney,

 

The goal is to not use a loop.

Is this part of your homework description? That's nonsense…

 

in other programming languages, for a 2D array 'A',  one could write max(A) and it returns the max of each coumn.

When you goal is to find a programming language, that allows the solution without using loops, you should use any of those languages, where "max(A)" not just provides the max value but also the max value of each column. IMHO I find a function "max(array)" misleading when it does not give the max value, but the max value of some arbitrary subarrays…

 

The LabVIEW solution to find the max of each row/column is to use an autoindexing FOR loop with ArrayMinMax inside…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 4 of 14
(7,957 Views)

This is not homework. I am trying to find a more efficient way of finding the max of each row in a 2D array other than using a FOR loop. If I use the MATLAB block in LabVIEW, I can write m = max(A, [], 2) to get the max of each row in A. However, the execution time to pass the data back and forth between MATLAB and LabVIEW is too long for my application. Using a FOR loop in LabVIEW is also too time consuming. If I perform the task in the MATLAB environment, the time is only a fraction of the time it takes to execute in LabVIEW. The problem is that I need LabVIEW to do the data acquisition, but the processing time in LabVIEW is excedingly long using indexed arrays in a FOR loop.

0 Kudos
Message 5 of 14
(7,942 Views)

Hi jmountney,

 

what size is your array?

 

The LabVIEW compiler is pretty smart about working with autoindexing FOR loops!

 

This one executes on my laptop in less than 50µs:

check.png

(For a 1000×1000 array it takes ~2ms.)

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 6 of 14
(7,936 Views)

@jmountney wrote:

This is not homework. I am trying to find a more efficient way of finding the max of each row in a 2D array other than using a FOR loop. If I use the MATLAB block in LabVIEW, I can write m = max(A, [], 2) to get the max of each row in A. However, the execution time to pass the data back and forth between MATLAB and LabVIEW is too long for my application. Using a FOR loop in LabVIEW is also too time consuming. If I perform the task in the MATLAB environment, the time is only a fraction of the time it takes to execute in LabVIEW. The problem is that I need LabVIEW to do the data acquisition, but the processing time in LabVIEW is excedingly long using indexed arrays in a FOR loop.


I'm very curious what your code looks like then.  When done properly, that should take next to no time.  Please provide an example.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 7 of 14
(7,933 Views)

 The array size is 50k rows x 100 columns. As an example I have attached a VI that demonstrates what I am attempting to achieve. This process takes a minimum of 35 ms to execute. Equivilantly, in MATLAB, I can write:

 

A = rand(50e3, 100);
tic
m = max(A,[],2);
1e3*toc

 

which takes less than 3 ms to execute.

 

Does LabVIEW automatically take advantage of multicores? Are there ways to make this computation more efficient?

0 Kudos
Message 8 of 14
(7,913 Views)
Solution
Accepted by jmountney

Hi jmountney,

 

using parallel execution brings execution time from 27ms to 5ms on my 8-core i7:

check.png

 

Does LabVIEW automatically take advantage of multicores?

General answer: Yes.

More specific answer: it helps to have a basic understanding of what the compiler is doing under the hood…

 

In your example you have a loop with just one function inside. This will manily run on just one core - unless you tell LabVIEW to use more cores as shown!

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 9 of 14
(7,907 Views)

Thank you.

Message 10 of 14
(7,896 Views)