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
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.

  • Similar Content

    • By Yean95
      Hello ! I've been trying to solve this problem for two days and I really need help on this.
      So I have a weekly progress table as below :
      As long as there's already a non-empty column for the PES #, the rest of the empty column after that non-empty column needs to show 0%. How should I do this?

       
      I need to make it into this table as below:

       
      Thank you so much in advance !
    • By hooovahh
      View File Hooovahh Array VIMs
      Here is the Hooovahh Array VIMs.  This initial release contains 14 VIMs for manipulating array data, which are intended to replace OpenG functionality, but with the added benefit of data type propagation, and increased performance using newer array manipulation techniques.  In later versions other Array manipulation functions were added moving all the OpenG stuff to their own palette.
      Most of the OpenG functions are unchanged, but a few use the newer conditional and concatenating tunnels.  And a few functions have added performance based on other inputs.  For instance the Delete Array Elements can operate in a more efficient way if the input indexes are already sorted.  The Filter 1D array can also be more efficient if the input is known to not contain any duplicates.
      Because these packages contain VIMs, they require LabVIEW 2017 or newer.  Having these functions be VIMs mean all functions work with various array data types.  Included functions are:
      Conditional Auto-Indexing Tunnel Delete Elements from (1D or 2D) Array Filter 1D Array Index (1D or 2D) Array, Scalar, Row, Column Remove Duplicates from 1D Array Reorder (1D or 2D) Array Reverse 1D Array Slice 1D Array Sort (1D or 2D) Array Convert 1D to 2D Convert 2D to 1D Find Subarray Force Array Min/Max Size Foreign Key Sort Submitter hooovahh Submitted 10/11/2017 Category *Uncertified* LabVIEW Version 2018 License Type BSD (Most common)  
    • By hooovahh
      Here is the Hooovahh Array VIMs.  This initial release contains VIMs for manipulating array data, which are intended to replace OpenG functionality, but with the added benefit of data type propagation, and increased performance using newer array manipulation techniques.  In later versions other Array manipulation functions were added moving all the OpenG stuff to their own palette. 
      Version 2.0 changed the suffix naming standard.  Updating may mean replacing calls to the new versions since the name on disk has changed.  This was for consistency and I'm sorry for breaking compatibility.  The added type defs in 2.0 may break compatibility too but these help avoid code breaking bugs since VIMs allowed any data type previously.
      Most of the OpenG functions are unchanged, but a few use the newer conditional and concatenating tunnels.  And a few functions have added performance based on other inputs.  For instance the Delete Array Elements can operate in a more efficient way if the input indexes are already sorted.  The Filter 1D array can also be more efficient if the input is known to not contain any duplicates.
      Because these packages contain VIMs, they require LabVIEW 2017 or newer.  Having these functions be VIMs mean all functions work with various array data types.  Included functions are:
      Conditional Auto-Indexing Tunnel Delete Elements from (1D or 2D) Array Filter 1D Array Index (1D or 2D) Array, Scalar, Row, Column Remove Duplicates from 1D Array Reorder (1D or 2D) Array Reverse 1D Array Slice 1D Array Sort (1D or 2D) Array Convert 1D to 2D Convert 2D to 1D Find Subarray Force Array Min/Max Size Foreign Key Sort
    • By WILSTi
      Hello,

      I'm developing an application to play "Battleship" with another opponent through serial communication. I currently have the following difficulty:
       I have not been able to fill the Array with my opponent's ships, I can modify a position, but by changing the position, the result of the previous position returns to its original value.
      VI operation:

      For my turn, using the radio buttons, I select the coordinate and press the SEND button. My opponent answers me with a 1 if I hit or with a 0 if I failed, this data is what I couldn't keep in the array.
      Best regards,
      3Q.vi
    • By Chen Li
      The program obtains the number of messages in the hardware buffer through ZCAN_GetReceiveNum function, and then calls ZCAN Receive to complete the received data. When the amount of data reaches 1000 frames / s, the program will crash.
      UINT ZCAN_Receive(CHANNEL_HANDLE channel_handle,  ZCAN_Receive_Data* pReceive,  UINT len,  INT wait_time = -1);

      I think there is something wrong with creating the parameter array ZCAN Receive Data* pReceive. Has anyone ever encountered a similar problem?
       
      Receive.zip
×
×
  • Create New...

Important Information

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