Color Code MatLab Histogram Based on a Condition of a 2x2 Matrix - matlab

if I have a 2x2 Matrix such as
1 2
4 7
And I want to plot a histogram based on the condition that the diagonals should be the color green, and everything else except the diagonals should be red, how do I do that in Matlab.
I have tried separating the 2x2 matrix into two different vectors representing the diagonal and the others (except diagonals), but when I do:
hist(diagonals)
hist(others)
It doesn't really work and seems to just randomly color code things into one histogram (I do want just the one histogram though.) How do I do this correctly?

Found a solution to my problem, using hold on fixes this issue
histogram(diagonals, 'FaceColor', 'g')
hold on
histogram(others, 'FaceColor', 'r')
hold on

Related

Is it possible to change the 'colormap' scale in Matlab?

I have 2 matrices. Matrix A contains values between 0 and 1 and matrix B contains values between 0 and 90. I would like to display an image with a different color for the numbers in each matrix.
When I use the colormap function with:
figure; colormap(jet); imshow(A);
The image displayed has several levels of gray, when I am supposed to have several colors (because I am using jet).
When I use the colormap function with:
figure; colormap(jet); imshow(B);
The image displayed is completely white, probably because my values are higher than 64 (which is the max of jet).
How can I solve these two problems? I read a lot of tutorials in several forums but I can't find the answer...
Thank you very much for answering my problem!
Just normalize the matrix by its max value if the values are more than 1. So for your B matrix try:
imshow(B/max(B(:)))
You can specify the colormap scaling and the number of actual colors within the colormap like so:
figure; imshow( A, [0 1], 'Colormap', jet(100) );
figure; imshow( B, [0 100], 'Colormap', jet(100) );
The jet(100) indicates 100 unique colors within the colormap to be used.
You are using the wrong function for the task in hand.
imshow expects an N by M by 3 array input, of the RGB channels of an image. When you use a 2D matrix the function assumes it's a grayscale image (it's like replicating it to 3 identical matrices to create these three channels - if all the channels in RGB have the same values you get grayscale colors). You can use this function together with a colormap to get a colored matrix, but there are much more convenient alternatives.
One simple function for getting a colored representation of a matrix is imagesc or (image if you want to scale the values by yourself). This function takes the values in your matrix, and assign them a color from the colormap you choose:
A = rand(10);
figure; colormap(jet); imagesc(A);
Another option is pcolor, which works a little different but give a similar result. pcolor attach the values to the vertices of the cells (in oppose to the center, as imagesc does), and interpolate the color in each cell from its' vertices. The resulted colored matrix is always smaller in one row and one column because it takes n+1 points (values in the original matrix) to define n gaps (the cells in the colored matrix). Here is an example:
A = rand(10);
figure; colormap(jet); pcolor(A);
shading flat

Scatter plot color thresholding

I am trying to write a script to plot florescence intensity from some microscopy data as a scatter plot and threshold this data based on cells that respond greater than a certain amount in CFPMAX and plot these in green and cells that do not in red. When I try to plot this, I am unable to really assign proper colors to points and they end up being blue and red. I need each cell in the image to be assigned 4 values, (3 values for for each florescence channel and one value to determine whether or not it responded (green or red). Thus I was wondering if it was possible to assign the proper color to the 4th column of the matrix, or if I am going about this the wrong way all together. I have attached my code below.
MCHR=csvread('data1.csv');
MYFP=csvread('data2.csv');
MCFP=csvread('data3.csv');
CFPMAX=(max(MCFP))';
MCHMAX=(max(MCHR))';
YFPMAX=(max(MYFP))';
c=zeros(length(CFPMAX));
for i=1:length(c)
if CFPMAX(i)>40
c(i)='g'; %// green responders
else
c(i)='r'; %// red non-responders
end
end
MM=horzcat(MCHMAX,YFPMAX,CFPMAX,c);
scatter(MM(:,1),MM(:,2),100,MM(:,4),'filled','MarkerEdgeColor',[0 0 0])
title('Responders vs Non-Responders ')
xlabel('[TF1]') %// x-axis label
ylabel('[TF2]') %// y-axis label
As far as I can tell from the documentation, the input parameter c (assuming scatter(x,y,a,c,...)) can be one of:
A single character specifying a colour e.g. 'g' or 'r'. But just one single scalar colouring all of you points.
A single RGB triple colouring all of your points so [1,0,0] for red or [0,1,0] for green.
A three column matrix of RGB triples. This is likely what you want. I will demonstrate this to you.
A one column matrix of numbers which will colour the points according to a colormap. This one will also work for you but less explicitly. Incidentally, I would guess that this is option that MATLAB though your vector of characters was.
So in order to create the matrix of RGB triples you can modify your code to be
c=zeros(length(CFPMAX),3);
for i = 1:length(CFPMAX)
if CFPMAX(i)>40
c(i,:)=[0,1,0]; %// green responders
else
c(i,:)=[1,0,0]; %// red non-responders
end
end
However, you could actually do away with the for-loop completely in MATLAB and construct c in a vectorized approach using logical indexing:
c=zeros(length(CFPMAX),3);
c(CFPMAX > 40, 2) = 1; %// green responders
c(CFPMAX <= 40, 1) = 1; %// red responders
This is a more idiomatic way to do this in MATLAB.

matlab - plot square matrix by row

Plot uses columns to plot a square matrix, but I need it by rows.
plot(matrix(1:end,1:end), 1:columns)
works great when matrix is rectangular (plots each line of matrix with a different color), but when matrix is square is plots by column (as the documentation clearly states)
I don't want to break the command to a loop because then I will have to specify the color for each line plot.
Is there a way to change the plot defaults to prefer row in square matrices?
You can use a trick and add a line of NaN at the end of the array:
matrix(end+1,:) = NaN;
And now matrix is rectangular, but the NaN won't be displayed during plot.
Best,

Drawing a mesh of a triangular matrix in Matlab

I have been trying to figure out how to "hide" the useless part of the triangular matrix from my 3d plot in Matlab.
I have tried drawing a mesh just above that part of the graph with
'EdgeAlpha', 1, 'FaceAlpha', 1,'FaceColor','w','EdgeColor','none'
but it does not help. How should I do it instead?
The only method the semi-worked is using the color scale, but it did not work all the way, plus I need the black and white eps's, which will show the color as black even if it looks white originally...
This is my last hope ;)
Barbara
Short answer: replace "useless" data with the value nan because MATLAB does not plot data values that are nan.
Inserting nan values into the other half of the matrix should do the trick. See the example below - it's clunky, but should give the idea. I chose to multiply by nan, which I attained as shown, but there are half a dozen other things that came to mind.
% Create random data for illustration
data = tril(rand(50));
% I chose to divide by a lower triangular ones matrix (zeros above the
% diagonal) to get nan above the diagonal and ones below
nan_above_diag_ones_below = 1./tril(ones(50,50));
% Plot data with and without hiding the "useless part"
figure,
subplot(1,2,1), mesh(data), title('"useless" part shown')
subplot(1,2,2), mesh(data.*nan_above_diag_ones_below),
title('"useless" part hidden')

Relative Markersize in Matlab plots

I am trying to plot a matrix where each element is in one out of two states. (ising model..)
Now, I would like to have one state colored and the other one white. That works using
[i,j] = find(S);
figure(gcf);
plothandle = scatter(i,j);
axis([0 nNodes+1 0 nNodes+1]);
when S holds the Spins and one state is equal to 0. (find returns a matrix of only non-zero elements)
To have a useful plot, the sizes of the markers should be 1x1 in RELATIVE coordinates. So if the whole matrix S would be in a state non-zero, everything would be colored.
However, it seems like Matlab only allows MarkerSizes in points or inches. How could I solve this?
One idea I had was, that I find out the point-size of the axes and then can easily calculate how big my markers should be. Then I would have to create a callback function if I want to zoom in and so on. Also, I have not yet found a way (without the image acq. toolbox) to find out the absolute size of my axes.
To clarify what I want: How could I plot a chessboard using a matrix with 1 for black and 0 for white fields?
For displaying data of this sort I generally prefer IMAGE or IMAGESC to PCOLOR since PCOLOR won't display the last row and column of the matrix when using faceted shading (the default). Also, IMAGE and IMAGESC flip the y axis so the image more intuitively matches what you think of when looking at a matrix (i.e. rows start from 1 at the top). You can visualize your matrix like this:
S = round(rand(20)); %# Sample 20-by-20 matrix of ones and zeroes
imagesc(S); %# Plot the image
colormap([1 1 1; 0 0 0]); %# Set the colormap to show white (zero elements) and
%# black (non-zero elements)
And here's a sample image:
Just as a suggestion, you can try using pcolor instead of `scatter' Example:
pcolor(hadamard(20))
colormap(gray(2))
axis ij
axis square