How can I equate a character to an integer?

3 visualizaciones (últimos 30 días)
Stephen H
Stephen H el 30 de Ag. de 2018
Editada: Stephen H el 30 de Ag. de 2018
I'm trying to write a function to find the summation of an m by n matrix.
if true
% code
end
m = 3.6
n = 4
if m == true && n == true
for i = 1:m
for j = 1:n
A(i,j) = i + j;
end
end
else
disp('both m and n must be positive integers.')
end
end
  4 comentarios
Stephen H
Stephen H el 30 de Ag. de 2018
can i implement a function like mod, floor or fix to make it work?
madhan ravi
madhan ravi el 30 de Ag. de 2018
Yes because index cannot be a decimal value

Iniciar sesión para comentar.

Respuesta aceptada

Image Analyst
Image Analyst el 30 de Ag. de 2018
Try this. I do it both your way, and a more MATLABy way with meshgrid that avoids explicit loops.
m = 3 % Rows or y
n = 4 % Columns or x
if rem(m, 1) == 0 && rem(n, 1) == 0 && m >= 1 & n >= 1
% Double for loop way:
A = zeros(m, n);
for row = 1 : m
for col = 1 : n
A(row, col) = row + col;
end
end
% Vectorized, MATLAB way:
[C, R] = meshgrid(1 : n, 1 : m)
B = C + R
else
uiwait(errordlg('Both m and n must be positive integers.'))
end
A % Show in command window.
You get
B =
2 3 4 5
3 4 5 6
4 5 6 7
A =
2 3 4 5
3 4 5 6
4 5 6 7
  4 comentarios
Stephen H
Stephen H el 30 de Ag. de 2018
thank you very much, i think i got it now lol
madhan ravi
madhan ravi el 30 de Ag. de 2018
If the answer works accept the answer so that you appreciate the person’s effort and let others know there is a correct answer for the question.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by