Borrar filtros
Borrar filtros

Define an input that is ONLY a 3-by-4 matrix

1 visualización (últimos 30 días)
amateurintraining
amateurintraining el 26 de Sept. de 2017
Comentada: Jan el 27 de Sept. de 2017
Hi.
I have a function called modify that I want to perform a series of tasks. How do I specifiy my input so that only a 3-by-4 matrix can be entered as input matrix A?
The following is my code:
function [ output,A1,A2,A3,A4,A5,A6 ] = modify( A )
%modify a given 3-by-4 matrix A
% output: a one-dimensional array consisting of y1,y2,y3,and y4
y1=A(end);
y2=A(2,3);
A1=A(2:3,2:3);
A2=A(5:7);
A3=A;
A3(3,2)=6;
A3=[A3 zeros(3,1)];
A4=2*A;
A5=A';
A6=[4 0 3];
X=[3 3 3; 1 0 0; 2 5 1];
A6=[A6; X];
A6=A5.*A6;
A6=A6([2:end],:);
y3=size(A6,1);
y4=sum(A6>10);
output=[y1 y2 y3 y4];
end

Respuestas (1)

Cedric
Cedric el 26 de Sept. de 2017
Editada: Cedric el 26 de Sept. de 2017
You can use specialized functions described here:
but for something as small as that, I would just go for:
assert( isequal(size(A), [3, 4]), 'Error message here..' ) ;
at the top of the function. This is equivalent to the following conditional statement:
if ~isequal( size(A), [3, 4] )
error( 'Error message here ..' ) ;
end
but more concise/compact.
  1 comentario
Jan
Jan el 27 de Sept. de 2017
+1: Both methods are nice and efficient. Another method:
validateattributes(A, {'numeric'}, {'size',[4,6,2]})
This is very powerful and useful. But in consequence it has a certain overhead.

Iniciar sesión para comentar.

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