Rand error in a while loop. Number of elements not matching. Pls help
3 views (last 30 days)
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
0 Comments
Accepted Answer
Steven Lord
on 14 Jun 2018
Let's say that you had a small vector mt_carr.
mt_carr = [0 0.25 0.25 0.25];
What's the result of computing the cumsum of mt_carr?
cumsum(mt_carr)
ans =
0 0.2500 0.5000 0.7500
Suppose that the rand function were to return a value like 0.9:
R = 0.9;
Let's run your code using those variables. [I've fixed R instead of using rand to make it easy to reproduce the behavior you're seeing.]
y = find(R < cumsum(mt_carr), 1, 'first')
y =
1×0 empty double row vector
This makes sense. None of the elements of the cumsum vector are greater than the value of R.
What happens if I try to assign that index back into an element of a vector?
x = 1:5;
x(1) = find(R < cumsum(mt_carr), 1, 'first')
I receive a different error message than the one you received when I run this code in release R2018a. We enhanced that error message to be a little bit easier to understand at some point, but I don't remember the exact release. The error message as displayed in the Command Window doesn't have a line break, but I didn't want you to have to scroll to see the whole thing.
Unable to perform assignment because the left and right sides
have a different number of elements.
The left side x(1) refers to 1 element of x. The right side (the find call) returns an array with 0 elements. This won't work.
I can think of at least two ways you could make this work. The first would be to ensure that the largest element of mt_carr was greater than or equal to the maximum value rand can return. If you do this, your code will need to handle the situation where your vector contains a value greater than the number of elements in mt_carr.
x(1) = find(R < [cumsum(mt_carr) Inf], 1, 'first')
The second way is to use the discretize function. This will return NaN if rand returns a value outside the range of the edges vector cumsum(mt_carr).
x(1) = discretize(R, cumsum(mt_carr))
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
KSSV
on 14 Jun 2018
YOur trail is not effective..then..we cannot run the code...without data and all variable.
See Also
Categories
Find more on Creating and Concatenating Matrices 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!