# Z must be a matrix, not a scalar or vector.

2 views (last 30 days)
Wayne Y Chan on 3 Dec 2022
Commented: Voss on 3 Dec 2022
t=0:30
x=(1+0.25.*cos(45.*t)).*cos(t)
y=(1+0.25.*cos(45.*t)).*sin(t)
[X,Y] = meshgrid(x,y)
Z=t+2.*sin(45.*t)
surf(X,Y,Z)
not sure why it keep told me the "Z must be a matrix, not a scalar or vector."
Thanks for the help!!
Voss on 3 Dec 2022
Maybe you intend to make a line in 3D rather than a surface?
t = 0:30;
x = (1+0.25.*cos(45.*t)).*cos(t);
y = (1+0.25.*cos(45.*t)).*sin(t);
z = t+2.*sin(45.*t);
plot3(x,y,z)

Eric Delgado on 3 Dec 2022
You have have a grid created by meshgrid, so you need a matrix to fill this grid. Not a vector...
It is what it is! :)
t=0:30;
x=(1+0.25.*cos(45.*t)).*cos(t);
y=(1+0.25.*cos(45.*t)).*sin(t);
[X,Y] = meshgrid(x,y);
Z = randn(size(X));
surf(X,Y,Z)

VBBV on 3 Dec 2022
Edited: VBBV on 3 Dec 2022
t=0:30 % a vector
t = 1×31
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
x=(1+0.25.*cosd(45.*t)).*cosd(t);
y=(1+0.25.*cosd(45.*t)).*sind(t);
[X,Y] = meshgrid(x,y) % X and Y matrices
X = 31×31
1.2500 1.1766 0.9994 0.8221 0.7482 0.8201 0.9945 1.1680 1.2378 1.1623 0.9848 0.8081 0.7336 0.8021 0.9703 1.1367 1.2016 1.1254 0.9511 0.7784 0.7048 0.7685 0.9272 1.0832 1.1419 1.0665 0.8988 0.7335 0.6622 0.7200 1.2500 1.1766 0.9994 0.8221 0.7482 0.8201 0.9945 1.1680 1.2378 1.1623 0.9848 0.8081 0.7336 0.8021 0.9703 1.1367 1.2016 1.1254 0.9511 0.7784 0.7048 0.7685 0.9272 1.0832 1.1419 1.0665 0.8988 0.7335 0.6622 0.7200 1.2500 1.1766 0.9994 0.8221 0.7482 0.8201 0.9945 1.1680 1.2378 1.1623 0.9848 0.8081 0.7336 0.8021 0.9703 1.1367 1.2016 1.1254 0.9511 0.7784 0.7048 0.7685 0.9272 1.0832 1.1419 1.0665 0.8988 0.7335 0.6622 0.7200 1.2500 1.1766 0.9994 0.8221 0.7482 0.8201 0.9945 1.1680 1.2378 1.1623 0.9848 0.8081 0.7336 0.8021 0.9703 1.1367 1.2016 1.1254 0.9511 0.7784 0.7048 0.7685 0.9272 1.0832 1.1419 1.0665 0.8988 0.7335 0.6622 0.7200 1.2500 1.1766 0.9994 0.8221 0.7482 0.8201 0.9945 1.1680 1.2378 1.1623 0.9848 0.8081 0.7336 0.8021 0.9703 1.1367 1.2016 1.1254 0.9511 0.7784 0.7048 0.7685 0.9272 1.0832 1.1419 1.0665 0.8988 0.7335 0.6622 0.7200 1.2500 1.1766 0.9994 0.8221 0.7482 0.8201 0.9945 1.1680 1.2378 1.1623 0.9848 0.8081 0.7336 0.8021 0.9703 1.1367 1.2016 1.1254 0.9511 0.7784 0.7048 0.7685 0.9272 1.0832 1.1419 1.0665 0.8988 0.7335 0.6622 0.7200 1.2500 1.1766 0.9994 0.8221 0.7482 0.8201 0.9945 1.1680 1.2378 1.1623 0.9848 0.8081 0.7336 0.8021 0.9703 1.1367 1.2016 1.1254 0.9511 0.7784 0.7048 0.7685 0.9272 1.0832 1.1419 1.0665 0.8988 0.7335 0.6622 0.7200 1.2500 1.1766 0.9994 0.8221 0.7482 0.8201 0.9945 1.1680 1.2378 1.1623 0.9848 0.8081 0.7336 0.8021 0.9703 1.1367 1.2016 1.1254 0.9511 0.7784 0.7048 0.7685 0.9272 1.0832 1.1419 1.0665 0.8988 0.7335 0.6622 0.7200 1.2500 1.1766 0.9994 0.8221 0.7482 0.8201 0.9945 1.1680 1.2378 1.1623 0.9848 0.8081 0.7336 0.8021 0.9703 1.1367 1.2016 1.1254 0.9511 0.7784 0.7048 0.7685 0.9272 1.0832 1.1419 1.0665 0.8988 0.7335 0.6622 0.7200 1.2500 1.1766 0.9994 0.8221 0.7482 0.8201 0.9945 1.1680 1.2378 1.1623 0.9848 0.8081 0.7336 0.8021 0.9703 1.1367 1.2016 1.1254 0.9511 0.7784 0.7048 0.7685 0.9272 1.0832 1.1419 1.0665 0.8988 0.7335 0.6622 0.7200
Y = 31×31
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0205 0.0205 0.0205 0.0205 0.0205 0.0205 0.0205 0.0205 0.0205 0.0205 0.0205 0.0205 0.0205 0.0205 0.0205 0.0205 0.0205 0.0205 0.0205 0.0205 0.0205 0.0205 0.0205 0.0205 0.0205 0.0205 0.0205 0.0205 0.0205 0.0205 0.0349 0.0349 0.0349 0.0349 0.0349 0.0349 0.0349 0.0349 0.0349 0.0349 0.0349 0.0349 0.0349 0.0349 0.0349 0.0349 0.0349 0.0349 0.0349 0.0349 0.0349 0.0349 0.0349 0.0349 0.0349 0.0349 0.0349 0.0349 0.0349 0.0349 0.0431 0.0431 0.0431 0.0431 0.0431 0.0431 0.0431 0.0431 0.0431 0.0431 0.0431 0.0431 0.0431 0.0431 0.0431 0.0431 0.0431 0.0431 0.0431 0.0431 0.0431 0.0431 0.0431 0.0431 0.0431 0.0431 0.0431 0.0431 0.0431 0.0431 0.0523 0.0523 0.0523 0.0523 0.0523 0.0523 0.0523 0.0523 0.0523 0.0523 0.0523 0.0523 0.0523 0.0523 0.0523 0.0523 0.0523 0.0523 0.0523 0.0523 0.0523 0.0523 0.0523 0.0523 0.0523 0.0523 0.0523 0.0523 0.0523 0.0523 0.0717 0.0717 0.0717 0.0717 0.0717 0.0717 0.0717 0.0717 0.0717 0.0717 0.0717 0.0717 0.0717 0.0717 0.0717 0.0717 0.0717 0.0717 0.0717 0.0717 0.0717 0.0717 0.0717 0.0717 0.0717 0.0717 0.0717 0.0717 0.0717 0.0717 0.1045 0.1045 0.1045 0.1045 0.1045 0.1045 0.1045 0.1045 0.1045 0.1045 0.1045 0.1045 0.1045 0.1045 0.1045 0.1045 0.1045 0.1045 0.1045 0.1045 0.1045 0.1045 0.1045 0.1045 0.1045 0.1045 0.1045 0.1045 0.1045 0.1045 0.1434 0.1434 0.1434 0.1434 0.1434 0.1434 0.1434 0.1434 0.1434 0.1434 0.1434 0.1434 0.1434 0.1434 0.1434 0.1434 0.1434 0.1434 0.1434 0.1434 0.1434 0.1434 0.1434 0.1434 0.1434 0.1434 0.1434 0.1434 0.1434 0.1434 0.1740 0.1740 0.1740 0.1740 0.1740 0.1740 0.1740 0.1740 0.1740 0.1740 0.1740 0.1740 0.1740 0.1740 0.1740 0.1740 0.1740 0.1740 0.1740 0.1740 0.1740 0.1740 0.1740 0.1740 0.1740 0.1740 0.1740 0.1740 0.1740 0.1740 0.1841 0.1841 0.1841 0.1841 0.1841 0.1841 0.1841 0.1841 0.1841 0.1841 0.1841 0.1841 0.1841 0.1841 0.1841 0.1841 0.1841 0.1841 0.1841 0.1841 0.1841 0.1841 0.1841 0.1841 0.1841 0.1841 0.1841 0.1841 0.1841 0.1841
Z=X+2.*sin(45.*Y); % do you mean this ?
surf(X,Y,Z);

### Categories

Find more on Matrix Indexing in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!