For loop data into 21X21 matrix.

I am trying to write a basic code that can take inputs from a for loop that is running a geometric function and write them into a matrix that needs to be square. 2X2, 100X100, whatever. I cannot find where to even start to write loop data into matrices.

 Respuesta aceptada

James Tursa
James Tursa el 19 de Jul. de 2017
E.g., a basic outline:
M = number of rows
N = number of columns
result = zeros(M,N); % pre-allocate result
for n=1:N
for m=1:M
result(m,n) = whatever; % insert your calculation code here
end
end

2 comentarios

Ryan  Parrott
Ryan Parrott el 19 de Jul. de 2017
This is being used to model the geometric function of a suspension geometry. So to do that accuratly I need to be able to input both negative and positive travels into one of the for loops. How would this be possible to run say "-1" to 1" with .1 degree of change each time"?
James Tursa
James Tursa el 19 de Jul. de 2017
Simply create vectors for each of the loop indexes. E.g.,
row_vector = -7:6; % whatever
col_vector = -5:0.5:3; % whatever, doesn't even have to be integers
M = numel(row_vector);
N = numel(col_vector);
result = zeros(M,N); % pre-allocate result
for n=1:N
for m=1:M
% the following is some function of row_vector(m) and col_vector(n)
result(m,n) = whatever; % insert your calculation code here
end
end

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 18 de Jul. de 2017

Comentada:

el 19 de Jul. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by