LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Find Best Array size for n elements

I have a 1D array of N elements that I'd like to convert into a 2D array of elements nxm where n and m are within 1 of each other so that

N=1 -> n = 1 m = 1

N=2 -> n = 2 m = 1

N=(3-4) -> n = 2 m = 2

N=(5-6) -> n = 3 m = 2

N=(7-9) -> n = 3 m = 3

N=(10-12) -> n = 4 m = 3

ect.

 

There's definitely a formula to describe this, but I can't put it together for the life of me. Any help on the best way to do this in LabVIEW would be appreciated.

 

0 Kudos
Message 1 of 13
(3,234 Views)

Only considered this briefly but how about:

Given N:

1. Take sqrt(N).  Round up to next higher integer, call result J.

2. Candidate A: J x (J-1) array if J*(J-1) >= N, else...

3. Candidate B: J x J array will always be big enough

 

 

-Kevin P

 

CAUTION! New LabVIEW adopters -- it's too late for me, but you *can* save yourself. The new subscription policy for LabVIEW puts NI's hand in your wallet for the rest of your working life. Are you sure you're *that* dedicated to LabVIEW? (Summary of my reasons in this post, part of a voluminous thread of mostly complaints starting here).
Message 2 of 13
(3,222 Views)

squareish.png

EDIT:  OOPS.  remove the divide by two

2nd Edit:  This won't guarantee n and m are within 1 of each other for higher values of N

Message 3 of 13
(3,184 Views)

If n can be equal to or greater than m by 1, then here would be my solution.

Near Square.png

Help the Community (and future reviewers) by marking posts as follows:
If it helped - KUDOS
If it answers the issue - SOLUTION
Message 4 of 13
(3,169 Views)

What you describe actually can't be done; what you're really describing is getting an optimized array that is the smallest almost-square. You're still padding your data array because N cannot always (even in the examples you give) fill a rectangular array that is "close" to being square. I think the closest you're going to get is this:

Janky Array Reshaping Example.PNG

______________________________________________________________________
The avatar is for the best weapon in any game ever. But nobody goes there anymore.

CLAD, LV2013, LV2015
0 Kudos
Message 5 of 13
(3,130 Views)

Kashiruvana,

 

It can be done.  Once the m & n dimensions have been determined, initialize the m*n array with NaN then fill from the single dimension array.

Help the Community (and future reviewers) by marking posts as follows:
If it helped - KUDOS
If it answers the issue - SOLUTION
0 Kudos
Message 6 of 13
(3,106 Views)

You're still padding the array, as I said in my response. You have to have a rectangular array, and even from the original examples, if N=10 and you want to put it into a 3x4 array, you're changing your original N=10 array into an N=12 array padded with two NaNs at the end. You don't have to stop and take that step, obviously; you can write your N=10 array into a 3x4 array initialized with NaNs and it happens along the way. But you can't take any 1D array of length N and put it into a 2D array of nxm without padding for any arbitrary N. For any example, you're padding with (nm)-N NaNs (or zeroes or whatever you pick).

______________________________________________________________________
The avatar is for the best weapon in any game ever. But nobody goes there anymore.

CLAD, LV2013, LV2015
0 Kudos
Message 7 of 13
(3,086 Views)

Reshaping pads the array with zeroes as needed.

 

Here's what I would do.

 

 

Download All
Message 8 of 13
(3,080 Views)

Ooh, I like that. That's a good solution. (If I could mark it, I would!)

 

When I wrote mine, I was under a time crunch, so I was looking for something that would give me the mantissa and exponent in base 10 instead of base 2, and I didn't see anything like that. So I came up with what I have, which works but definitely makes me uncomfortable--I mean, what? Convert to a string, then pick a magic index to check the char-to-num value of that, then... If I came across that in someone else's code that I had to refactor or use, I'd be mad, lol. I like yours a lot better.

______________________________________________________________________
The avatar is for the best weapon in any game ever. But nobody goes there anymore.

CLAD, LV2013, LV2015
0 Kudos
Message 9 of 13
(3,055 Views)

@kashiruvana wrote:

Ooh, I like that. That's a good solution. (If I could mark it, I would!)

 

 


Why can't you?  Just use the Accept as Solution button beneath the post.

0 Kudos
Message 10 of 13
(3,052 Views)