Subscript indices must either be real positive integers or logicals?

5 visualizaciones (últimos 30 días)
Zach Koenig
Zach Koenig el 25 de Abr. de 2016
Respondida: Image Analyst el 26 de Abr. de 2016
When I input option 2, then input the matrix, everything goes fine until the transpose function. It reads an error message titled, "Subscript indices must either be real positive integers or logicals." How do I get rid of this?
Marix = [1 2 1 ; 0 2 0 ; 2 1 1]
Thanks for the help
clc;
fprintf('Matrix operations (0 to end program):\n');
fprintf('1 create a matrix\n');
fprintf('2 operation on 1 matrix\n');
fprintf('3 operation on 2 matrices\n');
i = input('Enter option: ');
while i ~= 0
if i == 1
fprintf('Create matrices\n')
fprintf('Create a matrix of ones, zeroes, random numbers, identity\n')
rows = input('Enter number of rows:');
columns = input('Enter number of columns:');
ones_matrix = ones(rows,columns)
zeros_matrix = zeros(rows,columns)
random_matrix = rand(rows,columns)
identity_matrix = eye(rows,columns)
end
if i == 2
fprintf('Operations on one matrix\n')
matrix_1 = input('Enter matrix 1: ');
if abs(det(matrix_1)) <= 1e-15
fprintf('Singular matrix, no determinant or inverse\n')
else
fprintf('Determinant, inverse\n')
determinant = det(matrix_1)
inverse = inv(matrix_1)
end
fprintf('Multiply matrix1 by a scalar\n');
scalar = input('Enter scalar:');
scalar_mult = matrix_1 * scalar
fprintf('Transpose matrix 1\n');
transpose = transpose(matrix_1)
end
if i == 3
fprintf('operations on two matrices\n')
matrix_1 = input('Enter matrix 1: ');
matrix_2 = input('Enter matrix 2: ');
fprintf('add, element by element multiplication\n');
[a b] = size(matrix_1);
[c d] = size(matrix_2);
if a ~= c
fprintf('Matrices do not have same dimensions cannot add, or do element by element multiplication');
else
fprintf('Add matrix1 and matrix2\n');
matrix_add = matrix_1 + matrix_2
fprintf('Element by element multiplication\n');
ele_mult = matrix_1 .* matrix_2
end
if b ~= c
fprintf('Columns of matrix1 ~= rows of matrix2, cannot do matrix multiplication\n');
else
matrix_mult = matrix_1 * matrix_2
end
end
if i>3 || i<0
fprintf('Option entered: %d is invalid\n',i)
end
fprintf('Matrix operations (0 to end program):\n');
fprintf('1 create a matrix\n');
fprintf('2 operation on 1 matrix\n');
fprintf('3 operation on 2 matrices\n');
i = input('Enter option: ');
end

Respuestas (2)

Star Strider
Star Strider el 25 de Abr. de 2016
You are ‘overshadowing’ the transpose function here:
transpose = transpose(matrix_1);
If you name a variable the same as a function, the next time MATLAB encounters the name, it will assume you are referring to the variable, not the function.
Solution: Rename the variable so there is no conflict with the function name.
  2 comentarios
Zach Koenig
Zach Koenig el 25 de Abr. de 2016
No matter what I name the variable I run into this problem.
Star Strider
Star Strider el 26 de Abr. de 2016
Editada: Star Strider el 26 de Abr. de 2016
I get the error with this code:
matrix_1 = [2 1; 3 4]*0.1
transpose = transpose(matrix_1)
transpose = transpose(matrix_1) % Same ‘Subscript indices must either be real positive integers or logicals.’ Error Here
but not with this:
matrix_1 = [2 1; 3 4]*0.1
transpose = transpose(matrix_1)
clear transpose % Clear The ‘transpose’ Variable (Function Unaffected)
transpose = transpose(matrix_1) % No Error Here
also:
matrix_1 = [2 1; 3 4]*0.1
trps = transpose(matrix_1) % No Error Here
trps = transpose(matrix_1) % No Error Here
trps = transpose(matrix_1) % No Error Here
This leads me to believe my ‘overshadowing’ hypothesis is correct.

Iniciar sesión para comentar.


Image Analyst
Image Analyst el 26 de Abr. de 2016

Categorías

Más información sobre Linear Algebra 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