Converting a 1x1 Vector into a Scalar Number
69 次查看(过去 30 天)
显示 更早的评论
I have written the following code: number_trials=10000; number_generations=21;
number_resistant_colonies_Darwin=zeros(number_trials,1);
generation_number=zeros(number_trials,1);
final_number_bacteria=150*2^21;
alpha=1e-8;
for j=1:number_trials
NWild=zeros(1,number_generations+1);
NMutant=zeros(1,number_generations+1);
NWild(1)=150;
NMutant(1)=0;
for i=1:number_generations
New_born_mutants=poissrnd(alpha*NWild(i));
NWild(i+1)=(2*NWild(i))-New_born_mutants;
NMutant(i+1)=(2* NMutant(i))+New_born_mutants;
end
number_resistant_colonies_Darwin(j)=NMutant(end);
k=find(NMutant,1);
generation_number(j)=k
end
I keep receiving this error message: "Unable to perform assignment because the left and right sides have a different number of elements. Error in (line 22) generation_number(j)=k"
I understand that k is a 1x1 vector so it can't be entered into generation_number. How can I convert k into a scalar so that the number can be inputed into the generation_number matrix?
Thanks!
1 个评论
Stephen23
2018-5-14
编辑:Stephen23
2018-5-14
"I understand that k is a 1x1 vector.... How can I convert k into a scalar..."
MATLAB does not have a data class named "scalar": a scalar is simply a 1x1x1x1x... array. Likewise a matrix is simply an RxCx1x1x1x1x... array. There are no special data types for these, their classification as scalar/vector/matrix/... depends solely upon their sizes, but does not change anything about the class/type of the object in memory. Learn more about basic MATLAB classes:
回答(2 个)
Walter Roberson
2018-5-14
In one of your runs, there are no non-zero entries in NMutant, so k is returning empty.
If you look at the examples in https://www.mathworks.com/help/stats/poissrnd.html you can see that poissrnd() can return entries that are 0.
0 个评论
Aakash Deep
2018-5-14
I agree with Stephen Cobeldick comments that there is no data class as scalar/vector/matrix, it all depends on their dimensions. The error you are getting is because the variable k is empty that's why it's giving an error of dimension mismatch. Try to correct the dimension of k and your code will work.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Numeric Types 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!