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.

 采纳的回答

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 个评论

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

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by