For Loop help for function input
조회 수: 2 (최근 30일)
이전 댓글 표시
Hello,
I am trying to make a for loop to create inputs for a function, and store outputs - there are 4 inputs, and manually I would type in:
ones(4,1) in the function which is called main, so it looks in the input window as: main(ones(4,1))
This gives 4 outputs, which I would like to store in an excel file later on. However, I want to vary these inputs, and change all 4 of them to make 4^4 combinations (256) with two numbers (1 or 5). So for example:
[1 1 1 1];
[5 1 1 1];
[1 5 1 1];
[5 5 1 1];
[1 1 5 1]; ...................
as you can see, doing this manually is quite tedious - I know a for loop helps here and I can make 256 iterations run by
for i = 1:256
(this is main body)
.....
end
I am confused on whether I should have a nested loop to do this, and how I can store these values in separate rows for each iteration. Any help will be appreciated.
댓글 수: 0
채택된 답변
David Hill
2022년 12월 2일
r=randi(2,5,4);
r(r==2)=5
댓글 수: 4
David Hill
2022년 12월 3일
s=zeros(16,4);
for i=1:16
r=randi(2,1,4);%assumed output from another function
r(r==2)=5;
s(i,:)=r;
end
s
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 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!