Macro for random reshuffle of z-stack

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Macro for random reshuffle of z-stack

odellr
   I'm relatively new to writing macros, and was wondering if anyone has already written an ImajeJ macro which takes a z-stack of 'x' slices, and reshuffles the stack with each slice in a new, random position? Kind of like reshuffling a deck of cards. I know this probably seems very straightforward to experienced programmers, but as I have little-to-no experience in code/macro writing, I'm not sure how to proceed. Any suggestions or pointers would be great.  Thanks!
 
Ryan
Reply | Threaded
Open this post in threaded view
|

Re: Macro for random reshuffle of z-stack

ctrueden
Hi Ryan,

> was wondering if anyone has already written an ImajeJ macro which
> takes a z-stack of 'x' slices, and reshuffles the stack with each
> slice in a new, random position? Kind of like reshuffling a deck of
> cards.

The most popular algorithm for shuffling is called Fisher-Yates [1].

Fortunately for you there is macro function: Stack.swap(i, j).

 So the implementation is dirty easy:

    for (i = nSlices - 1; i >= 1; i--) {
        j = round(i * random() - 0.5) + 1;
        Stack.swap(i, j);
    }

Regards,
Curtis

[1] https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle



On Tue, Feb 4, 2014 at 10:11 AM, odellr <[hidden email]> wrote:

>    I'm relatively new to writing macros, and was wondering if anyone has
> already written an ImajeJ macro which takes a z-stack of 'x' slices, and
> reshuffles the stack with each slice in a new, random position? Kind of
> like
> reshuffling a deck of cards. I know this probably seems very
> straightforward
> to experienced programmers, but as I have little-to-no experience in
> code/macro writing, I'm not sure how to proceed. Any suggestions or
> pointers
> would be great.  Thanks!
>
> Ryan
>
>
>
> --
> View this message in context:
> http://imagej.1557.x6.nabble.com/Macro-for-random-reshuffle-of-z-stack-tp5006401.html
> Sent from the ImageJ mailing list archive at Nabble.com.
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html