# Multiply/divided two tables

110 views (last 30 days)
Yogesh Bhambhwani on 13 Nov 2020
Commented: Steven Lord on 13 Nov 2020
How would I mulyiply two tables togther. I have one table and I took two columns from the table and I want to multiply them togther. Also how would I divide as well thanks.

Steven Lord on 13 Nov 2020
The arithmetic operators aren't defined for table or timetable arrays, in part because they often contain data for which those arithmetic operators don't really make sense.
patients = table(LastName,Gender,Age,Height,Weight,Smoker,Systolic,Diastolic);
ans = 8x8 table
LastName Gender Age Height Weight Smoker Systolic Diastolic ____________ __________ ___ ______ ______ ______ ________ _________ {'Smith' } {'Male' } 38 71 176 true 124 93 {'Johnson' } {'Male' } 43 69 163 false 109 77 {'Williams'} {'Female'} 38 64 131 false 125 83 {'Jones' } {'Female'} 40 67 133 false 117 75 {'Brown' } {'Female'} 49 64 119 false 122 80 {'Davis' } {'Female'} 46 68 142 false 121 70 {'Miller' } {'Female'} 33 64 142 true 130 88 {'Wilson' } {'Male' } 40 68 180 false 115 82
What should the variable LastName in patients2 in the example below contain? How about the variable Smoker?
%{
patients2 = 2*patients
%}
As Jon and KALYAN ACHARJYA said, you can operate on the variables in a table. One thing their posts did not show is that you don't have to store the results to a temporary variable then put them into a table. You can stick them directly back into the table.
patients.AgeNextYear = patients.Age + 1;
patients.WeightKG = patients.Weight/2.205;
patients.HeightM = patients.Height/39.37;
patients.BMI = patients.WeightKG./(patients.HeightM).^2;
% Extract just a few variables to display from patients
patients2 = patients(:, ["LastName", "Age", "AgeNextYear", "HeightM", "WeightKG", "BMI"]);
ans = 8x6 table
LastName Age AgeNextYear HeightM WeightKG BMI ____________ ___ ___________ _______ ________ ______ {'Smith' } 38 39 1.8034 79.819 24.542 {'Johnson' } 43 44 1.7526 73.923 24.066 {'Williams'} 38 39 1.6256 59.41 22.482 {'Jones' } 40 41 1.7018 60.317 20.827 {'Brown' } 49 50 1.6256 53.968 20.423 {'Davis' } 46 47 1.7272 64.399 21.587 {'Miller' } 33 34 1.6256 64.399 24.37 {'Wilson' } 40 41 1.7272 81.633 27.364
Steven Lord on 13 Nov 2020
Plotting isn't defined for table arrays for much the same reason that the arithmetic operators aren't. But just as with the arithmetic operators, you can index into the table and call the plot function on the retrieved data.
patients = table(LastName,Gender,Age,Height,Weight,Smoker,Systolic,Diastolic);
plot(patients.Height, patients.Weight, 'o')

### More Answers (1)

Jon on 13 Nov 2020
% suppose you have a table T1 and T2 you could do something like
A = [T1.myColumnName1 T1.myColumnName2]
B = [T2.myColumnName3 T2.myColumnName4]
C = A.*B % assuming you want element by element multiplication
D = A./B % assuming you want element by element division
Jon on 13 Nov 2020
Assuming as Steven Lord suggests that you want to add the new computed column in the same table following your verbal description:
T.newColumn = myNumericalValue*T.oneColumn.*T.anotherColumn./T.yetAnotherColumn
If you just want to have that as a vector you could assign the left hand side to your vector, v
v = myNumericalValue*T.oneColumn.*T.anotherColumn./T.yetAnotherColumn