Multiply/divided two tables
67 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Yogesh Bhambhwani
el 13 de Nov. de 2020
Comentada: Steven Lord
el 13 de Nov. de 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.
0 comentarios
Respuesta aceptada
Steven Lord
el 13 de Nov. de 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.
load patients
patients = table(LastName,Gender,Age,Height,Weight,Smoker,Systolic,Diastolic);
head(patients)
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"]);
head(patients2)
3 comentarios
Steven Lord
el 13 de Nov. de 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.
load patients
patients = table(LastName,Gender,Age,Height,Weight,Smoker,Systolic,Diastolic);
plot(patients.Height, patients.Weight, 'o')
Más respuestas (1)
Jon
el 13 de Nov. de 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
3 comentarios
Jon
el 13 de Nov. de 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
Ver también
Categorías
Más información sobre Tables en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!