Jump to content

avarage 2D arrays


Recommended Posts


I have written a vi that converts the acquired data to 2D array. Now, I want to sum a specified number of these 2D arrays and average them. I have put the vi inside a while loop, and tried to use shift register to sum the arrays and calculate their average by dividing their summation to the number of times that loop runs. What comes out from the loop, would be a 2D array that is average of all 2D arrays after that the loop is stopped by user. I think need to define a zero 2D array as an initiator for the shift register, and I don't know how to do this.

Or, if you know a better way to do this, I would appreciate your suggestion.

Thank you in advance for your time and help

I have started labview self-training 3weeks ago and I am new in this field, so plz forgive me, if I ask some questions that are so basic for some of you.


Edited by star14
Link to comment

If you know the dimensions of your 2D array, that is if the size of the 2D array is constant, you can initialize the array using the Initialize Array function in the arrays palette, and feed that into your shift register. The default array is 1D, but you can make it 2D by simply adding a dimension input. Then feed your zero constant into the Element input and the resulting output will be a 2D arrayof zeroes with the dimension constants you wire to the dimension size inputs. It is generally good practice to initialize an array so that LabVIEW will allocate the space for it up front, eliminating less efficient on-the-fly array sizing operations.


Hope this helps.

Link to comment

Hi daren924,

Thank you very much for your detailed explanation.So based on this, I think I can't initialize my shift register using a zero array. So, I have tried other ways , but I am sure that some thing is wrong here, the way that I wire the outputs to the + has problem. I think I add some arrays twice because of my wiring. I have started labview self-training recently and my major is not programming so I might make mistakes that are so basic for you I am sorry.

I can't attach my vi here to show you



Link to comment



So based on this, I think I can't initialize my shift register using a zero array. 

What reason do you feel will keep you from initializing the array? 

Do you know the array dimensions?

Will the array be a constant size?


If the array size will be changing, averaging arrays of different sizes becomes a much more complex issue.

Link to comment

I'll take the "rube" that properly handles 'NaN' six days a week and twice on Sunday...

Oh fiddle sticks.  Well I guess this method still works for numeric types that don't support things like NaN and Inf.


EDIT: By the way this would make a good XNode.  Wire in an array of any size, or any data type, and it will return an array of the same size and data type, with the default value for that data type.

Link to comment

Okay because I like you guys (and I like XNodes) I've made an XNode that takes an input array of size 1 or greater, and will output an array of that same type, of that same size, but the values of the elements will be the default for that data type.


It uses the method posted earlier by jcarmody.  Feel free to look at how it works by looking at the XNode Template VI.  Source is saved in 2013.

Initialize Array XNode.zip

Link to comment

Hi Daren924, Thomas Gutzler & Jcarmody

Thank you very much for your reply.

I want to capture "i" images, remove their red layer, convert to array, take average, and then convert the average to image.

I have written a vi, but I have problem to take average. I know that my vi is wrong, and its problem is the way that I take the average and my looping as well.

I want to write the program in such a way that as I hit the Boolean, it start taking "i" images (i is defined by the user), remove their red layer, convert them to array, average them , convert the average array to image and then I stop it by hitting the Boolean again.

I have a snippet of my vi and tried to put it here, but it doesn't allow me to post it here, I dont know how to do it. 

Many thanks for your help


Link to comment

Hi Thomas,

I have developed my VI in a different way, but it has problem. I have sent it to you via messenger, bcoz I couldn't upload it here.

I would appreciate it a lot if you have look at it and help me to fix it. I want to learn the software by asking from experts like you and correcting my mistakes.

Thanks for your time and help

Link to comment

I don't have access to the IMAQ library, so I can't run your vi, which I've attached.



From looking at it, I see that I misunderstood your description about what to do with the red layer. You extract the red layer and average over multiple red layers. In my VI I remove the red layer from an image and average over the modified images. That's easily fixed (see 2nd attachment).

Create modify average image.vi


I see several problems in your code:

1) You shouldn't have a hard coded 3000x3000 array sitting on your BD. Use "Initialise array" to create that at runtime

2) You have several uninitialised shift registers. That's going to cause weird behaviour

3) You're adding two U8 arrays together. The result is another U8 array. That's going to cause you a LOT of pain, because in U8: 200+240=184. What you want to do is have the array that receives the sum be U16 or SGL.

4) The outer shift register you're using (I don't understand what for) is never reset. So you'll just keep adding images together in a weird way which is probably not what you want.

5) You should look into Event Structures



Link to comment
  • 2 weeks later...

Join the conversation

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

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.