Help creating a loop

조회 수: 1 (최근 30일)
Noah Wilson
Noah Wilson 2019년 6월 24일
댓글: Bob Thompson 2019년 6월 25일
Attached is the code in question. Currently I have the variables 'th' and 'Io' currently at one value. I would like to change them so that the user can input a range of values for them. I am thinking that it will be easiest to create a loop for the ranges but I am unsure how to do that and implement it in the existing code where those variables are used. Any help is greatly appreciated. Thanks in advance.

채택된 답변

Bob Thompson
Bob Thompson 2019년 6월 24일
I did not look in great detail at your code, but it seems like you are always going to define your variables ahead of time because the number of input arguments will always be less than seven for how you have things set up. You will need to have th and Io put into the function inputs if you want them to be user input. (I am assuming you know that, but reminding you.)
There are a couple of different options of how you can handle an array input like that. Loops are always an option, either inside or outside the function.
% Outside
th = [1 2 3];
Io = [1 2 3];
for i = 1:length(th)
for j = 1:length(Io)
[outputs] = KraKro_DVedit(Otherinputs,th(i),Io(j));
end
end
Inside looks pretty much the same, define th and Io outside, pass the entire set in, the inside have the same pair of loops, but inside the loops will be the calculations involving th and Io.
It seems like most of your calculations with th and Io are just addition, subtraction, multiplication, and division. It is possible to submit an array into this type of process, and have Matlab automatically evaluate the elements separately, but you will need to make sure the th and Io are the same size, and that all of your mathematical operators become element operators (* becomes .*, addition/subtraction are the same).
Keep in mind that the array math will only look at elements in the same location (th(1) to Io(1), not th(1) to Io(2)). If you want to compare all elements of th and all elements of Io, then I suggest trying the following outside of the function:
[TH,IO] = meshgrid(th,Io);
c=cat(2,TH',IO');
d=reshape(c,[],2);
When you run the function with this, just pass d(:,1) as th input, and d(:,2) as Io input.
  댓글 수: 4
Noah Wilson
Noah Wilson 2019년 6월 25일
I understand how you got to the range and the loop but I am lacking the understanding of how to implement that into the existing code to get the same results as before. Sorry for the confusion.
Bob Thompson
Bob Thompson 2019년 6월 25일
To get the same result as before, set th = [132.8]; and Io = [4.1e+8];
If you want it setup for user input they have access to those values outside of the function and can change them accordingly, or you can set it up as a input prompt as well, if you would like.
th = input('Enter the th values: ');
Io = input('Enter the Io values: ');

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by