Matrix dimension must agree
11 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Chanaka Navarathna
am 2 Feb. 2019
Kommentiert: Star Strider
am 2 Feb. 2019
Hi,
I need some help with troubleshooting this code. It keeps giving me the error
Error in Assigment2problem1function (line 16)
It11=I0f.*10.^(-A1);
Error using .*
Matrix dimensions must agree.
function [ Absorbance1,Absorbance2 ] = Assigment2problem1function(I0,E1,E2,l,Nrep)
%I0=100000, E1=10000, E2=20000, l=1, Nrep=3
% Detailed explanation goes here
% A = ECl beer-lambert law
% A = -log T = -log(It/Io)=log(I0)-log(It)
% A = Absorbance
%It = Intensity of Transmitted photons
%I0 = Initial Intensity of photons
% Assigment2problem1function(100000,10000,20000,1,3)
C=10:10:50*(10^-6);
SD=sqrt(I0);
for i=1:Nrep
I0f=I0+rand(1,Nrep)*SD;
A1=E1*C*l;
It11=I0f.*10.^(-A1);
SD21=sqrt(It11);
It12=It11+SD21.*rand(1,1);
Absorbance1=log(I0)-log(It12)
A2=E2*C*l;
It21=I0f.*10.^(-A2);
SD21=sqrt(It21);
It22=It21+SD21.*rand(1,1);
Absorbance2=log(I0)-log(It22)
end
0 Kommentare
Akzeptierte Antwort
Star Strider
am 2 Feb. 2019
The problem is the way you define ‘C’. MATLAB interprets this as going from 10 to 50E-6 in steps of 10. It never iterates at all, because 50E-6 is less that the first iteration step of 10, so it produces an empty value. Even when you correct that by multiplying the entire vector (not only the last value) by 1E-6, you then get another dimension disagreement because with the ‘Nrep’ value you posted creates a vector length of 3 while ‘C’ has a length of 5.
The easiest way to solve these problems is to define ‘C’ as:
C = linspace(10, 50, Nrep)*1E-6;
Your function then runs without error. I leave it to you to determine that it produces the correct outputs.
3 Kommentare
Star Strider
am 2 Feb. 2019
As always, my pleasure.
My calculation for ‘C’ automatically increases its length for any value of ‘Nrep’ you specify. Without the 1E-6 factor, it becomes simply:
C = linspace(10, 50, Nrep);
so there is no need to fix the length of any of your vectors.
Weitere Antworten (1)
Adam Danz
am 2 Feb. 2019
Bearbeitet: Adam Danz
am 2 Feb. 2019
The error message is telling you the problem. In the line below, you're multiplying two matrices I0f and 10.^(-a1). If you look at their sizes (see example below), you will see that they have different sizes.
It11=I0f.*10.^(-A1);
% Look at sizes
size(I0f)
size(10.^(-A1)
There are 2 types of matrix multiplications. When you include the dot (.*), you are multiplying two matrices element-wise. So A(1,1) will be multiplied by B(1,1). The matricies must be the same size. Here's an example:
[1 2 3; 4 5 6] .* [1 1 1; 2 2 2]
ans =
1 2 3
8 10 12
Without the dot (*), you're doing a standard matrix multiplication where the the sizes must be [n, m] and [m, p]. Here's an example
[1 2 3; 4 5 6] * [1 1; 2 2; 3 3]
ans =
14 14
32 32
Depending on which type of multiplication you're seeking, you'll need to make sure the sizes of the matricies are appropriate.
Siehe auch
Kategorien
Mehr zu Particle & Nuclear Physics finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!