Converting a 1x1 Vector into a Scalar Number

조회 수: 69 (최근 30일)
Bianca Batista
Bianca Batista 2018년 5월 14일
답변: Aakash Deep 2018년 5월 14일
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
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
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.

Aakash Deep
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.

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by