calling a function n times

조회 수: 16 (최근 30일)
john birt
john birt 2011년 9월 3일
say I have a function called myfun which has no arguments but returns a number, and I call it and assign the value to 'y', like
y=myfun()
now say I want to call it 'n' times to get a vector 'y' with 'k' entries. Ive tried
k=(1:10);
y(k)=myfun();
but this does not work, what do I do?
p.s. my exact function code is
function [critical_region] = rubbish()
Y=sort(randn(50000,100));
Z=zscore(Y);
n = size(Y,1);
j = 1:n;
result = -n-(1/n)*sum(bsxfun(@times,j.'*2-1, log(normcdf(Z,0,1))+log(1-normcdf(Z(end:-1:1,:),0,1))));
critical_region = prctile(result,[85, 90, 95, 97.5, 99]);

답변 (2개)

Walter Roberson
Walter Roberson 2011년 9월 3일
for k=1:10
y(k) = myfun();
end
  댓글 수: 2
john birt
john birt 2011년 9월 3일
that produced an error
??? In an assignment A(I) = B, the number of elements in B and
I must be the same.
Error in ==> rubbish2 at 2
t(k) = rubbish();
Walter Roberson
Walter Roberson 2011년 9월 3일
Your initial description said that the function returns a number, but it does not: it returns a vector of numbers. Adjusting for that, and with the premise that it will always return the same number of elements:
for k=1:10
y(k,:) = myfun();
end

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


Andrei Bobrov
Andrei Bobrov 2011년 9월 3일
function critical_region = yourfun
Y=sort(randn(500,10));
Z=zscore(Y);
q=size(Y);
result = -q(1)-(1:2:q(1)*2)*(log(normcdf(Z,0,1))+log(1-normcdf(Z(q(1):-1:1,:),0,1)))/q(1);
critical_region = prctile(result,[85, 90, 95, 97.5, 99]);
end
for i1 = 10:-1:1
y(i1,:) = yourfun;
end

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by