Please, how do I fix a for loop to store some counters obtained from a while loop.
close all;clear;clc;
NumberofSimulation = 10; % Number of simulations
StoreCounter = zeros(1,NumberofSimulation); % Stores the counters,
% StoreCounter = [11
for kk = 1: length(NumberofSimulation)
counter = 0; % Initializes the counter
ii = 1;
while ii <= 20
temp = randi([25 40]);
if temp >= 30
counter = counter + 1;
end
ii = ii + 1;
fprintf('kk = %d, ii = %d \n',kk,ii)% iterates aonly once. Thus, can it
%iterate more.
end
StoreCounter(kk) = counter; % This does not work, please. Only the counter
% obtained by first simulation is stored in the vector, StoreCounter.
end

 채택된 답변

madhan ravi
madhan ravi 2018년 11월 30일
편집: madhan ravi 2018년 11월 30일

0 개 추천

length(NumberofSimulation) is one because it's a scalar not a vector nor a matrix thats why your for loop iterates once
for kk=1:NumberofSimulation %change your line to this

댓글 수: 2

Auwal Abdullahi
Auwal Abdullahi 2018년 11월 30일
Thnk you so much. Now it works.
madhan ravi
madhan ravi 2018년 11월 30일
Anytime :)

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

추가 답변 (1개)

Susana Merino-Caviedes
Susana Merino-Caviedes 2018년 11월 30일

0 개 추천

I think the problem is that NumberofSimulation is a scalar, so its length is 1. Therefore, the for loop:
for kk = 1: length(NumberofSimulation)
will just run once. Try changing it by:
for kk = 1: NumberofSimulation
so that it runs NumberofSimulation times.
Regards

카테고리

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

질문:

2018년 11월 30일

댓글:

2018년 11월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by