output variable size matrix, while its specified type is something else.

조회 수: 3 (최근 30일)
ameen
ameen 2014년 9월 10일
댓글: ameen 2014년 9월 11일
Hi There
I want to generate random number based on its probability and i found the following code online
for time1=0:10:100;
r=rand;
prob=[.5,.1,.4];
prob=cumsum(prob);
value=[6,7,8]; %values corresponding to the probabilities
ind=find(r<=prob,1,'first');
x=value(ind)
end
it is working fine on Matlab however on Simulink it gives me the following error:
''Data 'x' (#79) is inferred as a variable size matrix, while its specified type is something else. ''
I changed the size of x and makes it 'variable size' then i received the following message:
''Output 'x' (#79) has variable size but the upper bound is not specified; explicit upper bound must be provided. ''
What i Want is: generating one random number from (6, 7, 8) based on its probability at each for loop.
Many Thanks in advance.

채택된 답변

AJ von Alt
AJ von Alt 2014년 9월 10일
MATLAB Function Block is having difficulty inferring the size of x.
Replacing
x=value(ind);
with
x=value(ind(1));
should fix this.
  댓글 수: 3
AJ von Alt
AJ von Alt 2014년 9월 11일
It's not clear what the intended behavior is.
The entirety of the code in the MATLAB Function block is executed at each time step. Since x is overwritten at each iteration, only the last value calculated is ever output.
If you want to output the same value for 10 seconds and then draw a new random value, you could change the sample time of the block to match that or use persistent variables to store the last drawn random value and the time that it was drawn and then update them only when the change in time has surpassed a threshold.
function out = fcn( time )
persistent tLast
persistent rValLast
if isempty( tLast ) || ( time - tLast ) > 10
tLast = time;
rValLast = % update random value here
end
out = rValLast;
ameen
ameen 2014년 9월 11일
Thank you very much for your help, i really appreciate it.

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by