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!