I need some help with a loop. i have codes here:
RandomArray= randi([-100 100],20,5); % step 1
RandomArray=sort(RandomArray(:)); % step 2
RandomArray = reshape(RandomArray,5,20).';
y=mean(RandomArray,2); % step 3
[c,index] = min(abs(y-0)); % determines which row is closest to 0
RowNumber= index;
I have to repeat these steps 1000 times then plot RowNumber in a histogram. I know I can use a FOR loop but i'm a bit lost on how to assign the codes. Can someone help? please and thank you.

댓글 수: 4

James Tursa
James Tursa 2017년 2월 21일
Repeat these steps 1000 times and then do what with the results? Save everything into multi-dimensional arrays? Save only the index? Save only the rows associated with the index? Or what?
Anonymous Matrix
Anonymous Matrix 2017년 2월 21일
편집: Anonymous Matrix 2017년 2월 21일
then plot RowNumber in a histogram. *updated the question
Is this what you want to do?
N = 1000;
RowNumber = zeros(N,1);
for kk=1:N
RandomArray= randi([-100 100],20,5); % step 1
RandomArray=sort(RandomArray(:)); % step 2
RandomArray = reshape(RandomArray,5,20).';
y=mean(RandomArray,2); % step 3
[c,index] = min(abs(y-0));
RowNumber(kk) = index;
end
% Plot histogram
histogram(RowNumber);
Anonymous Matrix
Anonymous Matrix 2017년 2월 21일
Yes! i was mainly lost on where to put the kk. thanks!

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

답변 (1개)

Akira Agata
Akira Agata 2017년 2월 21일

1 개 추천

Just in case, let me copy&paste my comment to the answer section.
N = 1000;
RowNumber = zeros(N,1);
for kk=1:N
RandomArray= randi([-100 100],20,5); % step 1
RandomArray=sort(RandomArray(:)); % step 2
RandomArray = reshape(RandomArray,5,20).';
y=mean(RandomArray,2); % step 3
[c,index] = min(abs(y-0));
RowNumber(kk) = index;
end
% Plot histogram
histogram(RowNumber);

댓글 수: 1

Jan
Jan 2017년 2월 21일
편집: Jan 2017년 2월 21일
+1. Omit the funny "-0" from min(abs(y-0)).

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

카테고리

도움말 센터File Exchange에서 Spline Postprocessing에 대해 자세히 알아보기

태그

질문:

2017년 2월 21일

편집:

Jan
2017년 2월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by