05-23-2017 11:59 AM
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.
05-23-2017 12:12 PM
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
05-23-2017 01:31 PM - edited 05-23-2017 01:35 PM
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
05-23-2017 02:02 PM
If n can be equal to or greater than m by 1, then here would be my solution.
05-23-2017 07:54 PM
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:
05-24-2017 07:01 AM
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.
05-24-2017 05:25 PM
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).
05-24-2017 06:28 PM - edited 05-24-2017 06:30 PM
05-25-2017 10:32 AM
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.
05-25-2017 10:40 AM
@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.