This is a project for a class.
Three variables d_o_shaft, h and w are the inputs.
Two outputs are Defl and Nf.
I'd like to store the three inputs in a 3xn matrix every time they meet a certain criteria; 1.3 <= Nf < 1.31 and Defl < .01.
This is the basic structure of my code, I simplified the equations for visual ease.
for do_shaft = .04:.001:.14
for h = .01:.001:.10
for w = .01:.001:.10
Nf = func(inputs)
Defl = func(inputs)
end
end
end

 채택된 답변

Torsten
Torsten 2022년 4월 2일

0 개 추천

i = 0;
for do_shaft = .04:.001:.14
for h = .01:.001:.10
for w = .01:.001:.10
Nf = func(inputs)
Defl = func(inputs)
if Nf >= 1.3 && Nf <= 1.31 && Defl <= 0.01
i = i + 1;
matrix(1:3,i) =[do_shaft,h,w]
end
end
end
end

댓글 수: 4

Nicolas Caride
Nicolas Caride 2022년 4월 2일
Thanks for the input! I don't see that result in the workspace on the right- how can I access that matrix to see what kind of results I get?
Torsten
Torsten 2022년 4월 2일
편집: Torsten 2022년 4월 2일
Sorry, should be:
matrix(i,1:3) = [do_shaft,h,w]
instead of
matrix(1:3,i) = [do_shaft,h,w]
You can inspect the matrix after the loop.
Voss
Voss 2022년 4월 2일
@Nicolas Caride It is a good idea to initialize matrix to empty before the loop (as in my answer), especially if you are running this in the base workspace. For instance, if the conditions are never met, matrix will be whatever it was before you ran this code, which may be something completely unrelated or it may not exist at all. In general, without initializing first, you're attempting to add a column (or row) to a matrix that may be the wrong size, in which case you'll run into an error.
Nicolas Caride
Nicolas Caride 2022년 4월 2일
Thanks so much for the help. That was exactly what I needed.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Voss
Voss 2022년 4월 2일

0 개 추천

matrix = [];
for do_shaft = .04:.001:.14
for h = .01:.001:.10
for w = .01:.001:.10
Nf = func(inputs);
Defl = func(inputs);
if Nf >= 1.3 && Nf < 1.31 && Defl < 0.01
matrix(:,end+1) = [do_shaft; h; w];
end
end
end
end

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2022년 4월 2일

댓글:

2022년 4월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by