dede
dede

Reputation: 1171

raster diagrams in Matlab based on colormap

I would like to create raster plots in Matlab based on my colormap jet. My colormap jet goes from -3 (blue) to +3 (red). So, I would like to plot the exact value of my data (e.g., +1.20) as the corresponding colour on the colormap jet (e.g., yellow).

However, at the moment I have a structure A of 66x900 double with values of 0 and 1, and a structure B of 66x900 double with different values in each cell (e.g., 0.21, +2.45, -1.09, etc.).

What I need to do is (1) to replace the values equal to 1 in structure A with the exact values of structure B. (2) I would need to plot the new structure (e.g., C) which is made of zeros and of values from structure B.

The tricky part is that in the raster plot, I would only need to plot with the colour of the colourmap jet the values/cells of structure C that were taken from structure B (including when those values were equal to zeros). Those cells that are equal to zero (those that were not replaced with values from structure B, as they were zeros already in structure A) need to become white pixels.

Example:

 Structure A
 0 **1** 0 0 0 **1** 0 **1** **1**   0     0 
 0 **1** 0 0 0   0   0 **1** **1**   0     0 
 0   0   0 0 0   0   0 **1** **1** **1** **1** 
 0   0   0 0 0   0   0 **1** **1** **1** **1** 

 Structure B
  1.2   **1.34**  2.34  1.33   1.43 **-2.99**  2.44  **1.27** **-2.76**   0.09      0.00 
 -2.3   **0.45**  2.99  2.01  -2.19   -0.98    0.56 **-1.87**  **2.14**   0.00     -0.98 
 -0.3     1.20   -0.17  1.2   -2.33   -0.67    1.23  **1.45**  **1.08** **1.01**  **0.00**
  1.11   -0.78   -1.66 -0.88   0.12   -1.04   -0.55  **1.32**  **1.34** **1.00**  **1.97** 

 Structure C
  0   **1.34**  0 0   0 **-2.99**  0  **1.27** **-2.76**   0         0 
  0   **0.45**  0 0   0    0       0 **-1.87**  **2.14**   0         0 
  0     0       0 0   0    0       0  **1.45**  **1.08** **1.01**  **0.00**
  0     0       0 0   0    0       0  **1.32**  **1.34** **1.00**  **1.97** 

Upvotes: 2

Views: 135

Answers (2)

Tommaso Belluzzo
Tommaso Belluzzo

Reputation: 23675

Random values:

a = double(randi([0 1],[4 11]));
b = rand(4,11);

Then:

idx = find(a == 1);
a(idx) = b(idx);

And here you have your colormap.

To obtain the white pixels (NaN will just make them transparent so they will take the background color):

a(a == 0) = 1;

Then, plot it:

colormap(gray);
pcolor(a);

Upvotes: 0

Laure
Laure

Reputation: 373

(1) You want to multiply each element of the matrix A with its corresponding element in matrix B. This is done by adding a dot before the multiplication symbol (it also works for division and exponentials).

(2) NaN values are left blank in Matlab plot.

Here's a working example:

% Your data
A = [1 1 1 1 1 1; 0 0 1 1 1 1; 1 1 1 0 1 1; 1 1 1 1 1 1; 1 1 1 1 1 1; 1 1 1 1 1 1];
B = [1 2 3 4 5 2; 5 4 0 0 0 4; 1 2 3 4 1 2; 5 5 5 3 3 3; 5 2 2 2 3 3; 5 1 5 3 4 4];
% Set all elements equal to zero to NaN so Matlab doesn't draw them
A(A==0) = NaN;
% This returns C(i,j) = A(i,j)*B(i,j) for all elements. 
% A*B would do a matrix multiplication
C = A.*B;
%Plot
pcolor(C)

Upvotes: 2

Related Questions