Rand error in a while loop. Number of elements not matching. Pls help
Show older comments
I'm getting the following error once in like 50 times : In an assignment A(:) = B, the number of elements in A and B must be the same. Error in WMTC_2 (line 272) pos(i)=find(rand<=cumsum(mt_carr),1,'first');
while t_sum<=t_final-tf_idle
ctr3=ctr3+1;
vf_sum=0;
vf_bef=vf_ave;
i=i+1;
pos(i)=find(rand<cumsum(mt_carr),1,'first');
pos_mt(i)=find(rand<cumsum(mt_arr(:,pos(i))),1,'first');
t_sum=t_sum+mt_cat1(pos(i)).mtc(pos_mt(i)).t;
tbef_sum=t_sum;
zz=i;
for z=1:i
vf_sum=vf_sum+mt_cat1(pos(z)).mtc(pos_mt(z)).vm_ave;
end
vf_ave=vf_sum/i;
if vf_bef<vp_ave %if the average of current set of chosen mts is more or less than prelim. target, choose the next mt so as to decrease or increase the net ave speed respectively
if mt_cat1(pos(i)).mtc(pos_mt(i)).vm_ave<v_ave
t_sum=t_sum-mt_cat1(pos(i)).mtc(pos_mt(i)).t;
i=i-1;
ctr6=ctr6+1;
end
else %vf_bef>vp_ave
if mt_cat1(pos(i)).mtc(pos_mt(i)).vm_ave>v_ave
t_sum=t_sum-mt_cat1(pos(i)).mtc(pos_mt(i)).t;
i=i-1;
ctr7=ctr7+1;
end
end
end
Accepted Answer
More Answers (1)
KSSV
on 14 Jun 2018
This error pops when you initialize LHS matrix to some size..and try to fill a matrix into it with dimension. Like, check below:
A = zeros(2) ;
A(:) = rand(3)
To solve this, you have two options.
1. Try to know the exact dimensions which are going to fit/ fill into LHS and then initialize LHS.
2. If you are not sure of the dimensions, initialize LHS as a cell.
3 Comments
Nagesh A P
on 14 Jun 2018
Nagesh A P
on 14 Jun 2018
KSSV
on 14 Jun 2018
YOur trail is not effective..then..we cannot run the code...without data and all variable.
Categories
Find more on Univariate Discrete Distributions in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!