Converting a 1x1 Vector into a Scalar Number

69 views (last 30 days)
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 Comment
Stephen23
Stephen23 on 14 May 2018
Edited: Stephen23 on 14 May 2018
"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:

Sign in to comment.

Answers (2)

Walter Roberson
Walter Roberson on 14 May 2018
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.

Aakash Deep
Aakash Deep on 14 May 2018
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.

Categories

Find more on Numeric Types in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!