Search code examples
matlabrandomcolorsoctavepsychtoolbox

Random color matrix in matlab


I would like to generate a matrix of random color blocks -- 10 by 10 pixel blocks of random colors -- such as:

https://i.sstatic.net/Jlc8L.png

So far, I have generated random numbers and enlarged the matrix with kron:

https://i.sstatic.net/eBU0T.png

using:

I = kron(randn([10 10]), ones(10));
imshow(I);

I would like to add random colors to this, but:

I = kron(randn([10 10 3]), ones(10));

reports:

error: invalid conversion of NDArray to Matrix

How can I generate a matrix of random color blocks?

This is for use with Psychtoolbox-3 (DrawTexture does not seem able to scale up a 10x10 random color matrix to 100x100 without interpolation).


Solution

  • You might as well generate the matrix directly:

        R = rand(10,10,3);
        figure;imshow(R);
    

    In order to create a block matrix, use imresize:

        Rb = imresize(R,10,'nearest');