LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Best way to replace a portion of array


@dsavir wrote:

Hi,

 

I admit I've no idea what you're trying to do and why, but here is your code implemented using an in place structure. If the Center coordinate is given in 0 based indexing (so that in a 3x3 array, the center would be (1,1)) this gives the same result as your code. As I've no idea what parameters to put in this could be way off, but the in place structures used might give you an idea. Note that in the inner in place structure the split dimension is 1, and in the outer the split dimension is 0 (right click->split dimension).

 

Good luck,

Danielle

 


Thanks Dsavir. I tested your code and I could not get the correct result. By using the split function for IPE Structure, it actually takes all the remaining elements.

On the other hand, using the array subset function, we can take out a sub- array with specified length. The length is calculated using the "dist. from center". Also, I could see that you did not wire the "length calculated" to the split function.

 

I think it is possible to get the solution with two additional IPE structure inside the two structure you created. THis is to ensure the array  portion has specified lenght. But, I think the performance and memory usage would be higher with this solution. 

 

 

 

 

 

0 Kudos
Message 12 of 22
(1,713 Views)

@ghighuphu wrote:

It can be enhanced certainly.

 

- avoid building case structures for easy operations. There is a trade off [time-wise] between building the case structure and calculating some extra values. Benchmarking needed.

- use the in-place structure as dsavir has suggested. I modified his, becuase it was not working for me, and I had to use a lot of "transposing 2D array". Again, benchmarking needed for your case.

 

Enhance readability by using proper representations (I32 vs DBL for indexes).

 

I think the distance from the center (DFC) should be redefined. It should be one less. Which elemenets are selected for 0 and 1, in your case? IMHO, DFC=0: a single element / no elements around, DFC=1: 3x3 elements,... You have DFC=2: 3x3 elements. DFC=0:??? I couldn't bare this so, I redefined it for me.

 

If you are keen on the best timing, you might want to benchmark the three suggestons archived and attached below. Let us know.

 

Cheers,


 

Sorry ghighuphu, I could not open your program. Please save it again in labview 2011 format. thank you 😃

0 Kudos
Message 13 of 22
(1,713 Views)
Message 14 of 22
(1,701 Views)

@ghighuphu wrote:

Here you go, LV2010.


Thanks a lot ghighuphu for three solutions you suggested. Sorry for the delay. I was on holiday recently.

 

I tested the code with an array of 1000x1000 elements and dist. from centre of 500. 

Here are the performance result:

 

1) Using for loop : ~33ms

2) Using IPE Structure : ~48ms

3) Using Array subset/replace (ghighuphu): ~50ms

4) Using Array subset/replace (my version): ~40ms

 

Although the for loop gives the best performance, however, I opt to not using. Actually in my program, I have multiple arrays and all of them are using the same 

portion of array location. So, by having the information on how to substract the array subset, it actually helps the operation to other arrays which are being executed sequentially. Hence, using for loop may not be the best way of doing it. Sorry, if you could not understand this. =P

 

I realised that the result for IPE structure is not correct. Since the timing for using IPE structure and usin "Replace array subset" are quite identical, I opt to use the latter since it provides better readability and allows the same portion to be used again in other operations. 

 

I am also not sure why my version is faster than yours, but the use of max/min function to determine the centre of subarray is a great idea!

 

Thanks a lot for the effort. Really appreciate it.

0 Kudos
Message 15 of 22
(1,663 Views)
I highly suggest posting the VI you used for your timing test. Many times people don't have rhe right settings for the code and will get incorrect results.
0 Kudos
Message 16 of 22
(1,643 Views)

 


@Hooovahh wrote:
I highly suggest posting the VI you used for your timing test. Many times people don't have rhe right settings for the code and will get incorrect results.

Please have a look at the attached image. All the timing test were done in the same method as illustrated. Please let me know if the way it is implemented is incorrect. thanks

0 Kudos
Message 17 of 22
(1,605 Views)
That actually looks pretty good, the only thing I'd suggest is inlining that VI if it isn't, and turn off automatic error handling, and disable debugging, if they are on.
0 Kudos
Message 18 of 22
(1,590 Views)

I'm gazing at your results and cannot believe it. Is the for-loop algorithm really the best for your large matrices? Are they sparse matrices (with a lot of NaN or zeros)? I coded the for-loop algorithm only to introduce some outsider for your timing experiment.

 

Can any Knight or Expert give any explanation for this? Why should a brute-force for-loop algorithm be better than the code with the IPE structure? And the code using IPE structure even worse than the code with subset replace? The latter should be optimised so no data copies are made.

 

Cheers,

0 Kudos
Message 19 of 22
(1,580 Views)
Could you post here one or two of your large arrays, please, dear clonzz?
0 Kudos
Message 20 of 22
(1,571 Views)