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.

 채택된 답변

James Tursa
James Tursa 2017년 7월 19일

0 개 추천

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

Ryan  Parrott
Ryan Parrott 2017년 7월 19일
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에 대해 자세히 알아보기

태그

질문:

2017년 7월 18일

댓글:

2017년 7월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by