12-19-2024 07:31 AM
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:
Solved! Go to Solution.
12-19-2024 08:06 AM - edited 12-19-2024 08:06 AM
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):
12-20-2024 12:34 AM - edited 12-20-2024 09:54 AM
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:
Here are two ways to do the same! The top is arguably simpler and equivalent (unless you have NaNs somewhere)
12-20-2024 03:21 AM
Another shortcut ist the Matrix Size node:
12-20-2024 09:53 AM - edited 12-20-2024 10:14 AM
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.).