1
votes

I have a vector of N numbers ranging in values from 0 to 1. Is there a way of getting Matlab to create a 3 by N matrix of RGB values depending on the value of the index of the initial vector?

I would like the colours to range from blue for 0 to red for 1, but go through intermediate colours as in the jet colourmap. So far, I have successfully got Matlab to range from blue to red, but with purple elements in between.

Any help would be greatly appreciated.

Thanks, Ryan

Code so far:

%Adapt colour values so that they are between 0 and 1
ixcolours=(Ix(:,1)+abs(min(Ix(:,1))))/max(Ix(:,1)+abs(min(Ix(:,1))));
iycolours=(Iy(:,1)+abs(min(Iy(:,1))))/max(Iy(:,1)+abs(min(Iy(:,1))));

for i=1:471;
    if seed_locs(i,3)==20; %If we are in the 20th seed z slice
        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
    elseif test_locs(i,3)==20; %if we are in the 20th test z slice
        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

Update

Thanks for the help lhcgeneva and FirefoxMetzger, both of your algorithms are useful.

I have tried implementing FirefoxMetzger's code, but now I'm presented with an issue regarding imagesc. Before, my full code was:

 %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

where I would have a 2 by 5 subplot of a brain with data superimposed on top of it as squares through the plot command.

However, now if I try to use the RGB values, I either only get images of the brain, or just the plotted points, but no superimposing.

 %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

2 Answers

2
votes

MATLAB has a built-in for this. If N is the number of shades you want (471 in your case) you can do

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

then access c in every loop iteration via c(i, :). For reference see the man page ;).

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

Where vector is any Nx1 vector with entrys within [0,1]. Note, that the dummy figure is only needed because colormap will allways open a new figure, unless there is one open already. Depending on your setup you may be able to just use the middle line.

This works even if your vector is not sorted or the spacing is inhomogeneous.