From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Editing contents of a labview array

Solved!
Go to solution

Hi all,

sorry I am new here. I have an array I of indices and an array A of zeros, both integer valued. I am looking for a way to increment by one the elements of A that corresponds to the indices in I. In python for example it would be:

 

for i in range(0, len(A)-1):
    A[I[i]] += 1

or in C:

 for( i = 0; i < sizeof(I)/sizeof(I[0]); i++ ) {
    A[ I[i] ]++;
}

or in Matlab:

for ii = 1:numel(I)
    A(I(ii)) = A(I(ii))+1; 
end

So far I made use of a for loop and then I use a) a "In place element" structure with a Index/replace function or b) a "Replace array subset" function, and a postincrement function:Clipboard.png

The problem is, in both cases the output just keep in memory the last change, i.e. if

A = [0 1 2]
I = [0 0 1]

I would expect as an output:

[2 2 2]

while what I get is:

[0 2 2]

I hope I state the problem in a clear way. Sorry again I am new to this community. I tried to search for a solution but I guess I am missing something more fundamental here. Any help would be greatly apprciated

Best,

M

0 Kudos
Message 1 of 6
(4,060 Views)
Solution
Accepted by topic author mlgpzz

The fundamental thing you're missing are shift registers. Use these to store values between loop iterations. Otherwise you keep modifying the original array in each iteration.

 

Message 2 of 6
(4,049 Views)

thanks a lot dan_u

indeed, replacing with shift register does the job

Clipboard.png

Any idea which one of the two solutions I listed in the first post is the most efficient?

Best,

M

0 Kudos
Message 3 of 6
(4,028 Views)

As previously stated, you just need a shift register.

 

Also, you do not need to wire up the N on the FOR loop.  The reason is because the loop known how many iterations to perform based on the Autoindexing array.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 4 of 6
(4,025 Views)

@mlgpzz wrote:

Any idea which one of the two solutions I listed in the first post is the most efficient?


With the latest compiler, I am fairly certain it turns out to be the same machine code.  So then it just becomes a matter of readability.  Personally, I find the In Place Element Structure to be easier to look at.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 5 of 6
(4,021 Views)

Dan is correct.

Also you don't need to connect the "N" input. 

The "indicies" array is set to auto-index.

 

Example_VI_BD.png

--------------------------------------------------------------------------------------------------------------------------
Help the forum when you get help. Click the "Solution?" icon on the reply that answers your
question. Give "Kudos" to replies that help.
--------------------------------------------------------------------------------------------------------------------------
Message 6 of 6
(4,009 Views)