开发者

plot matrices in desired colors

开发者 https://www.devze.com 2023-03-18 17:57 出处:网络
how is it possible to assign certain color to a value from matrix. For example I have a 10by10 matrix with values from 0 to 9.

how is it possible to assign certain color to a value from matrix. For example I have a 10by10 matrix with values from 0 to 9. Afterwards I'd like to get a "chess board" where 0 = white, 1 = black, 2 = blue...etc...

2nd question if I run some operations where my matrices change with each loop and I run let's say 10 lops (k = 10) - is it possible to make a video out of this 10 plot pictures I'll be getting aft开发者_如何学Pythoner each loop. (I'm programming some kind of cellular automaton, so I'd like to see how the situation changes over time).

Thanks


Consider this example:

%# lets create a 10-by-10 matrix, of values in the range [0,9]
M = fspecial('gaussian',10,2.5);
M = (M-min(M(:))) ./ range(M(:));
M = round(M*9);

%# prepare video output
vid = VideoWriter('vid.avi');
vidObj.Quality = 100;
vid.FrameRate = 5;
open(vid);

%# display matrix
h = imagesc(M);
axis square
caxis([0 10])
colormap(jet(10))
colorbar

%# capture frame
writeVideo(vid,getframe);

%# iterate changing matrix
for i=1:50
    M = rem(M+1,10);          %# circular increment
    set(h, 'CData',M)         %# update displayed matrix

    writeVideo(vid,getframe); %# capture frame

    drawnow                   %# force redisplay
end

%# close and save video output
close(vid);

plot matrices in desired colors

You can use a custom colormap, simply create a matrix cmap of size 10-by-3, each row contains the RGB values, and pass it to the call colormap(cmap)


For MATLAB versions older than R2010b, you can use the avifile function, instead of VideoWriter:

%# prepare video output
vid = avifile('vid.avi', 'fps',5, 'quality',100);

%# iterations
for i=1:50
    %# ...

    %# capture frame
    vid = addframe(vid, getframe(gcf));

    drawnow
end

%# close and save video output
vid = close(vid);
0

精彩评论

暂无评论...
验证码 换一张
取 消