# 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

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

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

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

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

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

Oh shoot you are right! thank you!

Edited by bingbot

## Join the conversation

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

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

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

×
• ### Similar Content

• #### combo box Indexing combo Box for both value and String

By Mahbod Morshedi,

• 8 replies
• 5,763 views
• #### How to replace "0" for all empty cells in between two non-empty cells of table in Labview?

By Yean95,

• 5 replies
• 3,662 views
• #### [CR] Hooovahh Array VIMs

By hooovahh,

• 24 replies
• 8,185 views
• #### Hooovahh Array VIMs

By hooovahh,

• 6,485 views
• #### populate ring array with data, serial communication

By WILSTi,

• 0 replies
• 5,517 views
×