01-13-2017 05:24 AM
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:
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
Solved! Go to Solution.
01-13-2017 05:37 AM
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.
01-13-2017 06:00 AM
thanks a lot dan_u
indeed, replacing with shift register does the job
Any idea which one of the two solutions I listed in the first post is the most efficient?
Best,
M
01-13-2017 06:01 AM - edited 01-13-2017 06:02 AM
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.
01-13-2017 06:03 AM
@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.
01-13-2017 06:25 AM
Dan is correct.
Also you don't need to connect the "N" input.
The "indicies" array is set to auto-index.