02-21-2023 09:58 AM
Hi!
Something that I'm always struggling to predict is when I am supposed to transpose an array ...
You have a nice 2D array made of rows and columns, index that in a for and you get .... well let's run it quickly ... ah rows.
My problem is also kinda extended to 1D arrays. When are they 2horizontal or Vertical? Does it even matter?
I've done this test right now (because I have to work with matrix and was not getting the expected result.
And, to me, it doesn't really make sense and I'm looking for one...
The "transpose 2D array" here is important... otherwise I get an array of only 1 element...
I enter a "vertical array", make a matrix out of it that considers it a column vector, put it back into a2D array, but if I don't transpose it before indexing, I just get the 1.
If anyone can explain to me the logic behind all this, I'd be grateful.
Thanks,
Vinny.
02-21-2023 10:03 AM - edited 02-21-2023 10:03 AM
Hi Vinny,
@VinnyAstro wrote:
if I don't transpose it before indexing, I just get the 1.
If anyone can explain to me the logic behind all this, I'd be grateful.
That's you fault! 🙂
02-21-2023 10:35 AM
Well, array to matrix creates a column vector, so you have to transpose to get the original back. You can open it and look what it does.
If you were to use build array instead, all would be fine.
02-21-2023 11:10 AM
Unlike in e.g. Matlab, "vectors" are simple 1D arrays and there is no concept of row- vs. column-vector. (no matter in what direction you resize it on the front panel!)
A "matrix" is a special typedef'd 2D array (guaranteed to be 2D, LabVIEW will silently substitute linear algebra for some basic operations)
If you look at all the LabVIEW tools in the linear algebra palette, they will typically operate correctly with a 1D array input for e.g. matrix/vector mixed operations. LabVIEW knows what to do! So don't overcomplicate your code by artificially creating special vectors as matrix with one row or column. It does not matter!
In your above code, the compiler is actually smart enough to NOT actually rearrange the elements in memory and do a real transpose, but just flag the downstream code to swap indices instead.
Do you have any specific problem you are trying to solve?
02-22-2023 02:43 AM
Hi! And thanks for your replies!
@GerdW wrote:
That's you fault! 🙂
Never said it wasn't 😁
@GerdW wrote:
- You are using IndexArray with its default input values: it will index the first row - which only contains one element "1". When you want to index the first column then you need to wire a "0" to the column input of IndexArray!
- Why do you convert a 1D array into a matrix which is defined as 2D structure in the first step?
If you look at all the LabVIEW tools in the linear algebra palette, they will typically operate correctly with a 1D array input for e.g. matrix/vector mixed operations. LabVIEW knows what to do! So don't overcomplicate your code by artificially creating special vectors as matrix with one row or column. It does not matter!
In your above code, the compiler is actually smart enough to NOT actually rearrange the elements in memory and do a real transpose, but just flag the downstream code to swap indices instead.
Do you have any specific problem you are trying to solve?
So if I want to apply a transformation matrix to a "1D array vector" I just multiply them?
As I said just above, in my application I have a lot of actual vectors (the Sun-Earth vector; The Nadir Vector etc.) in a specific coordinate system. And I have a VI that generates a transformation matrix from this specific system to another that I need my vectors into.
Up until now, 1D arrrays as vectors were fine, but just because of this transformation matrix I need to look into it 🙂
02-22-2023 02:51 AM
Hi Vinny,
@VinnyAstro wrote:
- So Labview indexes first by rows and then by columns. I've noticed that when I use for instance 2 for loops. The thing is I just never remember it and always have to test first. Is there a logic behind it or is it just a convention?
- I "suddenly" have to work with some transformation matrices and vectors, while my vectors are 1D arrays, my algebra is a bit far back and I wanted to do some test when I realized that I don't know if 1D arrays are row-vectors or column-vectors....
02-22-2023 03:52 AM
I admit that I just look at the data and if it is coming out sideways, I suppose to transpose.
02-22-2023 05:48 AM - edited 02-22-2023 05:49 AM
@billko wrote:
I admit that I just look at the data and if it is coming out sideways, I suppose to transpose.
That's generally my approach too. 😁
I tried a few times to remember the actual order and every time I needed it I was back at the 50/50 guessing game. So I figured why bother to learn that if I have to figure it out again anyhow. 😁
The specific problem here seem to be that the OP would like to have horizontal and vertical vectors. Which is an alien concept in LabVIEW. A vector is a vector is a vector. Rather than worrying about this part you have to make sure to apply a vector to the 2D matrix in the right way depending on the operation you want.
02-22-2023 06:38 AM
I was annoyed by this a while ago, too.
For me, most of the time I generate Data from measurments, a 1D Array of Data.
In a while loop this results in a 2D Array. If you put this in a for loop, you get your 1D Array of Data out!
Most of the time I want to make a mean Array of that measured Data, to get the Data of one Sensor I have to transpose it. That's my approach to this, thinking about the generation of that data to know how to get what I want.
I never worked with matrix or vectors, just saying.
02-22-2023 06:40 AM
@rolfk wrote:
The specific problem here seem to be that the OP would like to have horizontal and vertical vectors. Which is an alien concept in LabVIEW. A vector is a vector is a vector. Rather than worrying about this part you have to make sure to apply a vector to the 2D matrix in the right way depending on the operation you want.
Yep ok, I probably got too influenced with matlab some time ago and I never done any algebra in Labview.
And that's where my rusty algebra has to come back to life haha