# Graph matrix B with colormap based on matrix A

1 view (last 30 days)
Matthew Coles on 5 Aug 2022
Answered: Voss on 6 Aug 2022
Here is my sample code:
%This contains a large matrix containing a series of x, y, and z values of
%a piece of data. This is an example set for simplicity.
B = [0 0 0; 2 0 0; 3 0 0; 0 1 1; 0 2 2; 0 3 3; 1 0 0; 1 1 1; 1 2 2; 1 3 3; 2 0 0; 2 1 1; 2 2 2; 2 3 3; 3 0 0; 3 1 1; 3 2 2; 3 3 3];
% This is a suspected antiderivative
A = [0 0 0; 2 0 0; 3 0 0; 0 1 1; 0 2 4; 0 3 9; 1 0 0; 1 1 1; 1 2 4; 1 3 9; 2 0 0; 2 1 1; 2 2 4; 2 3 9; 3 0 0; 3 1 1; 3 2 4; 3 3 9];
x = B(:, 1);
y= B(:, 2);
z = B(:, 3);
[xq, yq] = meshgrid(0:0.1:5, 0:0.1:5);
zq = griddata(x, y, z, xq, yq);
Warning: Duplicate data points have been detected and removed - corresponding values have been averaged.
mesh(xq, yq, zq)
Here is my code to create the colormap. When I apply this colormap, it obviously colors based on the height (z) of matrix B. I want this colormap to instead color the mesh of B based on the heights of matrix A. I want to do this because I need to superimpose the colormap of a suspected antiderivative (A) onto the graph of B. How would I do this, if this is even possible in Matlab?
grey = [0.7, 0.7, 0.7];
red = [1, 0, 0];
blue = [0, 0, 1];
pos = [linspace(grey(1), red(1))', linspace(grey(2), red(2))', linspace(grey(3), red(3))'];
neg = [linspace(blue(1), grey(1))', linspace(blue(2), grey(2))', linspace(blue(3), grey(3))'];
map = [neg; pos]; % Creates a colormap where blue represents negative values and reds represent positive values
caxis([-1, 1]);
colormap(map) Voss on 6 Aug 2022
cq = griddata(x,y,A(:,3),xq,yq);
mesh(xq,yq,zq,cq)

R2022a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!