LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Shift rows based on which index they have

Solved!
Go to solution

I have two arrays: a 2d array where each row corresponds to a group, and a 1d array where each element is the amount of rows one group has. so for instance an array of A = [2,3,4] would mean that group 1 has 2 elements, group 2 has 3 and group 4 has 4. I am trying to shift each group by one column where the first group starts off at the first column, so the second one will be on the second column but their position in the row will be the same. I have made an attempt of implementing this but it isn't working as I hoped. Could anybody help me out? I have attached my vi and an example of what the expected output should be:

 

Asasafuchi_0-1734615079159.png

 

0 Kudos
Message 1 of 5
(108 Views)
Solution
Accepted by topic author Asasafuchi

Hi Asa,

 


@Asasafuchi wrote:

I have two arrays: a 2d array where each row corresponds to a group, and a 1d array where each element is the amount of rows one group has. so for instance an array of A = [2,3,4] would mean that group 1 has 2 elements, group 2 has 3 and group 4 has 4. I am trying to shift each group by one column where the first group starts off at the first column, so the second one will be on the second column but their position in the row will be the same.


Suggestion (based on what I understood from your description):

 

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 2 of 5
(96 Views)

A Vi example with all empty arrays is not illustrative, Could you populate your controls with example data (make default), then describe what the correct output you expect (or manually fill the output array with the desired result as default!).

The data in your image is not sufficient, for example we cannot tell how big the arrays really are. (container size != array size!)

 

 

There is a lot of code smell:

 

  • Why would you need to take the array size twice in parallel?
  • Once is enough! Index array is resizeable, right?

Here are two ways to do the same! The top is arguably simpler and equivalent (unless you have NaNs somewhere)

 

altenbach_0-1734676439405.png

 

Message 3 of 5
(50 Views)

Another shortcut ist the Matrix Size node:

 

Matrix Size.png

Greets, Dave
0 Kudos
Message 4 of 5
(36 Views)

Here's what I would probably do. This solution offers significantly better in-placeness and code readability. The final 2D array size is fully determined before the loop even starts and all later operations require no memory management (Trimming and growing arrays in a tight loop can get expensive!). It also ensures that the inputs actually match (the sum of elements in the group array must match the number of rows in the 2D array. If you can guarantee that the inputs are always suitable, you can omit the comparison and case structure.).

 

altenbach_0-1734709741850.png

 

0 Kudos
Message 5 of 5
(13 Views)