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.

 Réponse acceptée

James Tursa
James Tursa le 19 Juil 2017

0 votes

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 commentaires

Ryan  Parrott
Ryan Parrott le 19 Juil 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"?
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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by