Matlab 以所需颜色打印矩阵
如何将特定颜色指定给矩阵中的值。 例如,我有一个10x10的矩阵,其值从0到9。 然后我想得到一个“棋盘”,其中0=白色,1=黑色,2=蓝色…等等 第二项问题 如果我运行一些操作,其中我的矩阵随着每个循环而变化,我运行,比如说10个lops(k=10)-是否有可能在每个循环之后,用这10张绘图图片制作一个视频。(我正在编程某种细胞自动机,所以我想看看情况是如何随时间变化的) 谢谢考虑一下这个例子:Matlab 以所需颜色打印矩阵,matlab,video,colors,matrix,Matlab,Video,Colors,Matrix,如何将特定颜色指定给矩阵中的值。 例如,我有一个10x10的矩阵,其值从0到9。 然后我想得到一个“棋盘”,其中0=白色,1=黑色,2=蓝色…等等 第二项问题 如果我运行一些操作,其中我的矩阵随着每个循环而变化,我运行,比如说10个lops(k=10)-是否有可能在每个循环之后,用这10张绘图图片制作一个视频。(我正在编程某种细胞自动机,所以我想看看情况是如何随时间变化的) 谢谢考虑一下这个例子: %# lets create a 10-by-10 matrix, of values in th
%# 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);
您可以使用自定义颜色映射,只需创建一个大小为10×3的矩阵cmap
,每行包含RGB值,并将其传递给调用colormap(cmap)
对于早于R2010b的MATLAB版本,您可以使用该函数,而不是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);
考虑这个例子:
%# 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);
您可以使用自定义颜色映射,只需创建一个大小为10×3的矩阵cmap
,每行包含RGB值,并将其传递给调用colormap(cmap)
对于早于R2010b的MATLAB版本,您可以使用该函数,而不是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);
谢谢,我只是不明白你为什么这么做:M=(M-min(M(:))/范围(M(:);当范围(M(:)是1,M-min(M(:)除以1是同一个数字时?为什么我写:vid=VideoWriter('vid.avi');matlab返回:???“char”类型输入参数的未定义函数或方法“VideoWriter”?@kojikurac:前三行只是为了创建一个示例矩阵,如您所述(包含从0到9的自然数)。这应该替换为您拥有的实际数据。另一部分,在MatlabR2010B中引入了VideoWriter。如果您有较旧的版本,可以使用较旧的
avifile
功能。我更新了答案以显示如何使用此函数。谢谢,我不明白您为什么这样做:M=(M-min(M(:))./range(M(:);当范围(M(:)是1,M-min(M(:)除以1是同一个数字时?为什么我写:vid=VideoWriter('vid.avi');matlab返回:???“char”类型输入参数的未定义函数或方法“VideoWriter”?@kojikurac:前三行只是为了创建一个示例矩阵,如您所述(包含从0到9的自然数)。这应该替换为您拥有的实际数据。另一部分,在MatlabR2010B中引入了VideoWriter。如果您有较旧的版本,可以使用较旧的avifile
功能。我将更新答案以显示如何使用此功能。