Jump to content

how to get last index of 2d array


Recommended Posts

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 by bingbot
Link to comment

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 by JKSH
Link to comment

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 by bingbot
Link to comment

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 by JKSH
Link to comment

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 by bingbot
Link to comment

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 by JKSH
Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.