Sizeof error and Z is a scalar/Vector but needs to be matrix

1 visualización (últimos 30 días)
Osiris Terry
Osiris Terry el 9 de Sept. de 2020
Respondida: Steven Lord el 9 de Sept. de 2020
% x + y, x >= 0 and y >= 0
% f(x,y) = x - y^2, x >= 0 and y < 0
% -x^2 + y, x < 0 and y >= 0
% x^2 + y^2, x < 0 and y < 0
%
% If x and y are vectors, then plot the 3D line.
% If x and y are matrices, then plot the surface.
% If x and y are 3 dimensions or more, then don't plot anything.
% Function Section
if size(x) == size(y)
for n = 1:numel(x)
if x(n) >= 0 && y(n) >=0
z(n) = x(n) + y(n);
end
if x(n) >= 0 && y(n) < 0
z(n) = x(n) - y(n)^2;
end
if x(n) < 0 && y(n) >= 0
z(n) = -x(n)^2 + y(n);
end
if x(n) < 0 && y(n) < 0
z(n) = x(n)^2 + y(n)^2;
end
end
else
error('Inputs must be the same sized arrays')
end
% Plot Section
if isvector(x) && isvector(y)
plot3(x,y,z)
end
if (ismatrix(x) && ismatrix(y))
if numel(sizeof(x)) < 3 && numel(sizeof(y)) < 3 % Check if greater than or equal to three dim <--Error
surf(x,y,z) %<--Error
end
end
end
I am currently having trouble on the two labeled lines above, I am trying to plot a surface and make sure that the dimensions of the matrices are less than 3 but I am getting an error that z is a scalar/vector when it needs to be a matrix. As for the if statement above it the error is
Undefined function 'sizeof' for input arguments of type 'double'.
Error in MyEx08 (line 48)
if numel(sizeof(x)) < 3 && numel(sizeof(y)) < 3
The comments at the start of the code explain what I am trying to do.

Respuestas (2)

Fangjun Jiang
Fangjun Jiang el 9 de Sept. de 2020
sizeof() is not a function. Maybe you want to use ndims()

Steven Lord
Steven Lord el 9 de Sept. de 2020
x = 1:10;
if any(size(x) < 3)
error('x has size less than 3 in at least one of its dimensions.')
end
Compare:
y = magic(5);
if any(size(y) < 3)
error('y has size less than 3 in at least one of its dimensions.')
end
But I'd just preallocate z to be a zeros array the same size as x and y.
z = zeros(size(x));
Then you just have to check if z isvector or if it has size < 3 in any of its dimensions.
You may also be interested in this documentation page to avoid your for loop.

Categorías

Más información sobre Numeric Types en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by