How to for loop through variables with two columns

11 visualizzazioni (ultimi 30 giorni)
Hey, New to matlab and struggling with looping through variables. I have four variables that consist of the M and C values for calibration equations (y=mx+c) on four sperate days the variables are in this form:
C_CalEquation =
491.8587 -200.3298
352.5991 -114.2691
493.8250 -207.9643
652.4300 -294.4432
632.3712 -287.7151
407.7223 -152.0045
D_CalEquation =
490.4674 -199.8723
352.2292 -114.3976
492.5757 -207.6984
653.8977 -295.1794
632.3712 -287.7151
407.7223 -152.0045
Where column 1 is m and 2 is c
i'm trying to loop through these four variables to convert voltage into mm using these equations. I'm trying to use this code:
for r=(A_CalEquation B_CalEquation C_CalEquation D_CalEquation)
P1mm=(Probe1.*r(1,1))+r(1,2)
P2mm=(Probe2.*r(2,1))+r(2,2)
P3mm=(Probe3.*r(3,1))+r(3,2)
P4mm=(Probe4.*r(4,1))+r(4,2)
P5mm=(Probe5.*r(5,1))+r(5,2)
P6mm=(Probe6.*r(6,1))+r(6,2)
however the r=(A_CalEquation B_CalEquation C_CalEquation D_CalEquation) only returns the variables as a single column
r =
495.8334
354.1180
494.9175
653.8977
642.7533
406.8550
how do i make the for loop recognise the variables as two columns and not just a single one??

Risposta accettata

Stephen23
Stephen23 il 25 Feb 2017
Modificato: Stephen23 il 25 Feb 2017
Welcome to using MATLAB! The basic data type of MATLAB is the array. In fact every numeric variable is an array: it might be scalar, vector, matrix, or ND array, but they are all the same type: numeric array. This means that MATLAB also works really well with arrays: many numeric functions accept array inputs, and basic mathematical operations can be performed on arrays, without needing to use loops. Writing code to use arrays is usually simple, neater, and faster than trying to use loops. This means that whenever you find yourself doing something like this:
P1mm=(Probe1.*r(1,1))+r(1,2)
P2mm=(Probe2.*r(2,1))+r(2,2)
P3mm=(Probe3.*r(3,1))+r(3,2)
P4mm=(Probe4.*r(4,1))+r(4,2)
P5mm=(Probe5.*r(5,1))+r(5,2)
P6mm=(Probe6.*r(6,1))+r(6,2)
then it is not a very efficient use of MATLAB. Also putting meta-data (e.g. '5mm', '6mm', etc) into variable names is a vary bad practice that will make your code much more difficult to write. Read this to know why:
Having lots of similar variables which are named with some incrementing value (like a pseudo-index) is also not a good practice:
Instead of using meta-data and psuedo-indices into your variable names, you should learn how to use arrays (and if required, indexing). When you operate on all of your data at once, without splitting it into smaller parts in a loop, then you will be able to use MATLAB effectively.
For example let put these into one array CalEq:
C_CalEquation = [...
491.8587 -200.3298
352.5991 -114.2691
493.8250 -207.9643
652.4300 -294.4432
632.3712 -287.7151
407.7223 -152.0045];
D_CalEquation = [...
490.4674 -199.8723
352.2292 -114.3976
492.5757 -207.6984
653.8977 -295.1794
632.3712 -287.7151
407.7223 -152.0045];
here we define the two data arrays:
CalEq = cat(3,C_CalEquation,D_CalEquation); % 3D array
Probe = randi(9,size(CalEq,1),1); % Probe in one vector
And now the calculation is very simple, it just requires one line:
out = bsxfun(@times,Probe,CalEq(:,1,:)) + CalEq(:,2,:);
The dimensions of the output correspond exactly to those of CalEq, so if you put D_Cal on the second "page" (ie. along the third dimension) of the ND array, then D_Cal's output will be the second "page" as well:
>> out(:,:,2)
ans =
3723.9
3055.7
777.45
5589.9
3506.5
255.72
This is not only simpler than using loops, it will probably be faster too. Also note that recent versions of MATLAB do not require bsxfun, as the broadcasting is now performed for all arithmetic operators.
PS: The easiest solution to your original question is to loop over indices, not the columns themselves:
sz = size(CalEq);
sz(2) = 1;
out2 = NaN(sz);
for k = 1:sz(3)
out2(:,:,k) = Probe.*CalEq(:,1,k) + CalEq(:,2,k);
end
This gives the same output as the solution using bsxfun and no loop, it is just more complicated:
>> isequal(out,out2)
ans =
1

Più risposte (0)

Categorie

Scopri di più su Function Creation in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by