bingbot Posted July 25, 2015 Report Posted July 25, 2015 (edited) 1: How to get last index, not last element, but last index number of an 2d array? Also 2: say 2d array has contents elephant crow dog goat giraffe eagle cat fly ant pig horse bee How to get that the bee is the 12th or 11th index in the array? (11 if you count elephant as zero index, 12 if you count elephant as first index), meaning I want the index number 11 or 12, not "bee" Edited July 26, 2015 by bingbot Quote
JKSH Posted July 26, 2015 Report Posted July 26, 2015 (edited) Hi, Unlike MATLAB, LabVIEW does not use linear indexing. Since you have a 2D array, each element is indexed using 2 numbers (think of it as the row number and column number). Also, LabVIEW's indexing starts from 0. So, the index of "bee" is (2, 3), not 11 or 12. You can use the Array Size Function to find the dimensions of the array (3x4), and then subtract 1 from each element to get (2, 3). Edited July 26, 2015 by JKSH Quote
bingbot Posted July 26, 2015 Author Report Posted July 26, 2015 (edited) Is there still any way to get 11 or 12 from that 2d array? I can think of one way, using a for loop and then iterating until the end of array becomes true, and then storing that iteration outside the for loop, at least I think that will work, but I would think there would be a better way of doing it Edited July 26, 2015 by bingbot Quote
JKSH Posted July 26, 2015 Report Posted July 26, 2015 (edited) Is there still any way to get 11 or 12 from that 2d array? I can think of one way, using a for loop and then iterating until the end of array becomes true, and then storing that iteration outside the for loop, at least I think that will work, but I would think there would be a better way of doing it Use the Array Size Function, and then multiply (Row Size) x (Column Size). Edited July 26, 2015 by JKSH Quote
bingbot Posted July 26, 2015 Author Report Posted July 26, 2015 (edited) That method will only work if I have that specific (And a few more cases) of rows/columns in the array. I would like to find a method that would work regardless of the number of rows/columns. If the array contents was: elephant crow dog goat seal giraffe eagle cat fly Panda ant pig horse bee crocodile then multiplying the row size by the column size would not get me the linear element of crocodile, which would be 14 Edited July 26, 2015 by bingbot Quote
JKSH Posted July 26, 2015 Report Posted July 26, 2015 (edited) That method will only work if I have that specific (And a few more cases) of rows/columns in the array. I would like to find a method that would work regardless of the number of rows/columns. That technique works for all numbers of rows and columns. (Row Size) x (Column Size) = (Number of Elements in the 2D Array) If the array contents was: elephant crow dog goat seal giraffe eagle cat fly Panda ant pig horse bee crocodile then multiplying the row size by the column size would not get me the linear element of crocodile, which would be 14 Do you want to start counting from 1 or 0? If you count from 1, then the last "index" is (Number of Elements in the 2D Array) If you count from 0, then the last "index" is (Number of Elements in the 2D Array)-1 Try it for different array sizes. You'll see that it always works. Edited July 26, 2015 by JKSH Quote
bingbot Posted July 26, 2015 Author Report Posted July 26, 2015 (edited) Oh shoot you are right! thank you! Edited July 26, 2015 by bingbot Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.