How to create new column formula that references other tables?
11 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I want to add a column to my table for "Region" based on the column "ID". I have a table that lists the IDs and the regions. However, I run into an error every time I try something I see here since they all require multiple lines or assume the tables are the same size (which they aren't).
This isn't my first problem of this type and I don't want to keep asking specific questions every time I need help doing a specific thing. I don't want a specific workaround or a function that does my thing without the tables. You can give me that, but it won't help me understand and I'll be back here once I need to do this kind of thing again. I'm clearly missing a core concept. How do you make tables reference each other?
3 commentaires
Image Analyst
le 17 Août 2018
Let's say column 1 of table A had values 2, 5, 8, 13. Then those numbers could reference rows 2, 5, 8, and 13 of table B, or columns 2, 5, 8, 13, of table B - whatever you want.
Réponses (1)
Steven Lord
le 17 Août 2018
>> load patients
>> patients = table(LastName,Gender,Age,Height,Weight,Smoker,Systolic,Diastolic);
>> patients.W_H = patients.Weight./patients.Height;
>> head(patients)
Here I used the variables Weight and Height from the patients table to compute the value for another variable, W_H, in that same table. But nothing's stopping me from putting the results of computations or operations using data from the patients table into a different table.
>> patients2 = table(LastName, Gender, Location);
>> patients2.BloodPressure = patients.Systolic + " over " + patients.Diastolic;
>> head(patients2)
Just be careful that the two table arrays have the same number of rows and that the rows are in the same order, or you might be putting the BloodPressure for patient Taylor in the row for patient Smith or you might have more BloodPressure readings than patients. The mismatched order will not cause MATLAB to throw an error, but it may cause an incorrect assessment of the patient's health. The mismatched sizes will cause MATLAB to throw an error.
If you're not sure that the rows are in the same order, consider using the joining functions on this documentation page like join, innerjoin, and outerjoin to combine the two table arrays together and make sure the data for the same patient in each smaller table gets stored in the correct row of the larger merged table.
If that's not what you meant when you said you wanted your table arrays to "reference each other", I second Image Analyst's request for clarification. If you describe your application in a bit more detail we may be able to offer more useful guidance on how to achieve your goals.
0 commentaires
Voir également
Catégories
En savoir plus sur Tables dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!