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.

 Accepted Answer

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 Comments

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

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!