在Matlab中从1乘N向量创建RGB矩阵

时间:2015-11-24 21:58:55

标签: matlab rgb matlab-figure

我有一个N个数字的向量,范围从0到1.有没有办法让Matlab根据初始向量的索引值创建一个3×N矩阵的RGB值?

我希望颜色的范围从蓝色0到红色1,但是要像喷射颜色图一样穿过中间色。到目前为止,我已经成功地将Matlab从蓝色变为红色,但中间有紫色元素。

非常感谢任何帮助。

谢谢, 莱恩

到目前为止

代码:

 %Create Figure with handle.
h5=figure('units','normalized','outerposition',[0 0 1 1]);
whitebg(h5,[0 0 0]);
subplot(2,5,1);
k=1;
for i=16:25
    subplot(2,5,k);
    imagesc(squeeze(ana(:,:,i)));
    title(['Z=',num2str(i)]);
    hold on
    colormap gray
    axis equal
    k=k+1;
end
%Adapt colour values so that they are between 0 and 1. We want to scale
%both data sets equally, so we find the smallest value across Ix and Iy. We
%also find what will be the new largest value across Ix and Iy, after we
%add the magnitude of the smallest value to make all numbers greater than
%or equal to 0.
absolutemin=min(min(Ix(:,1)),min(Iy(:,1)));
absolutemax=max(abs(absolutemin)+(max(Ix(:,1))),abs(absolutemin)+max(Iy(:,1)));

%Add the smallest value, and divide by the largest maximum value for both Ix
%and Iy.
ixcolours=(Ix(:,1)+abs(absolutemin))/absolutemax;
iycolours=(Iy(:,1)+abs(absolutemin))/absolutemax;

o=1;
for k=16:25; %For all 3D slices
    for i=1:471; %and for all x and y seed slices
        if k==seed_locs(i,3);
            subplot(2,5,o); %go to the corresponding z subplot
            plot(seed_locs(i,1),seed_locs(i,2),'MarkerFaceColor',[ixcolours(i) 0 1-ixcolours(i)],'MarkerEdgeColor',[ixcolours(i) 0 1-ixcolours(i)],'MarkerSize',10,'Marker','s') %plot the x and y seedlocs
            hold on
        end
    end

    for i=1:486;
        if k==test_locs(i,3);
            subplot(2,5,o);
            plot(test_locs(i,1),test_locs(i,2),'MarkerFaceColor',[iycolours(i) 0 1-iycolours(i)],'MarkerEdgeColor',[iycolours(i) 0 1-iycolours(i)],'MarkerSize',10,'Marker','s') %plot the x and y seedlocs
        end

    end
    o=o+1; %go to the next z subplot
end

更新

感谢lhcgeneva和FirefoxMetzger的帮助,你的两种算法都很有用。

我尝试过实施FirefoxMetzger的代码,但现在我遇到了有关imagesc的问题。之前,我的完整代码是:

 %Adapt colour values so that they are between 0 and 1. We want to scale
%both data sets equally, so we find the smallest value across Ix and Iy. We
%also find what will be the new largest value across Ix and Iy, after we
%add the magnitude of the smallest value to make all numbers greater than
%or equal to 0.
absolutemin=min(min(Ix(:,1)),min(Iy(:,1)));
absolutemax=max(abs(absolutemin)+(max(Ix(:,1))),abs(absolutemin)+max(Iy(:,1)));

%Add the smallest value, and divide by the largest maximum value for both Ix
%and Iy.
ixcolours=((Ix(:,1)+abs(absolutemin))+1/64)/(absolutemax+1/64);
iycolours=((Iy(:,1)+abs(absolutemin))+1/64)/(absolutemax+1/64);
h1x=figure();
colour_values_x=interp1((1:64)/64,colormap(h1x,'jet'),ixcolours);
close(h1x)
h1y=figure();
colour_values_y=interp1((1:64)/64,colormap(h1y,'jet'),iycolours);
close(h1y)

%Create Figure with handle.
h5=figure('units','normalized','outerposition',[0 0 1 1]);
whitebg(h5,[0 0 0]);
subplot(2,5,1);
k=1;
for i=16:25
    subplot(2,5,k);
    imagesc(squeeze(ana(:,:,i)));
    title(['Z=',num2str(i)]);
    colormap gray
    axis equal
    k=k+1;
end
figure(h5);
o=1;
for k=16:25; %For all 3D slices
    for i=1:471; %and for all x and y seed slices
        if k==seed_locs(i,3);
            subplot(2,5,o); %go to the corresponding z subplot
            plot(seed_locs(i,1),seed_locs(i,2),'MarkerFaceColor',[colour_values_x(i,1) colour_values_x(i,2) colour_values_x(i,3)],'MarkerEdgeColor',[colour_values_x(i,1) colour_values_x(i,2) colour_values_x(i,3)],'MarkerSize',10,'Marker','s') %plot the x and y seedlocs
            hold on
        end
    end

    for i=1:486;
        if k==test_locs(i,3);
            subplot(2,5,o);
            plot(test_locs(i,1),test_locs(i,2),'MarkerFaceColor',[colour_values_y(i,1) colour_values_y(i,2) colour_values_y(i,3)],'MarkerEdgeColor',[colour_values_y(i,1) colour_values_y(i,2) colour_values_y(i,3)],'MarkerSize',10,'Marker','s') %plot the x and y seedlocs
        end

    end
    o=o+1; %go to the next z subplot
end

我将有一个2 x 5的大脑子图,其中数据叠加在它上面,作为通过绘图命令的正方形。

然而,现在如果我尝试使用RGB值,我要么只获得大脑的图像,要么只是绘制的点,而不是叠加。

library(plyr)
excel_files <- all-of-your-file-names-here..
output_file_names <- output-file-names-here..

for(i in all_files) {
  mydata <- read.xlsx(excel_files[i], sheetName = "Gyffin")
  uptake <- mydata[23:28,]
  uptake_1 <- uptake[c(1,5,6,11,12,17,18,22,23)]
  uptake_2 <- rename(new-names-here..
  write.csv(uptake_2, output_file_names[i])
}

2 个答案:

答案 0 :(得分:2)

MATLAB有一个内置的功能。如果N是你想要的阴影数量(在你的情况下是471)你可以做

N = 471;
c = colormap(jet(N));

然后通过c(i, :)在每次循环迭代中访问c。 有关参考,请参阅man page;)。

答案 1 :(得分:0)

dummy_figure = figure();
color_values = interp1((1:64)/64,colormap(dummy_figure,'jet'),vector)';
close(dummy_figure);

其中,vector是任何Nx1向量,其中entys在[0,1]内。请注意,仅需要虚拟图形,因为色彩图将始终打开一个新图形,除非已经打开了一个。根据您的设置,您可以使用中间线。

即使您的矢量未排序或间距不均匀,这也可以正常工作。

相关问题