Clear Filters
Clear Filters

Dividing a row in a table by its first entry which results in a new column with a 1*1 table in each row. How can i change it to the number?

2 views (last 30 days)
Hi,
I want to normalize my row by dividing it by its first entry. Which I'm doing in the following way.
D1990.pricenorm = D1990(:,"priceadj")./D1990(1,"priceadj");
Where D1990 is the table, pricenorm the new column and priceadj the column I want to normalize.
My code gives me the right result but stores it as 1*1 table instead of just the number. I attached a picture to be clear what I mean.
How can I fix it? Thank you in advance.

Accepted Answer

Dyuman Joshi
Dyuman Joshi on 27 Sep 2023
Edited: Dyuman Joshi on 27 Sep 2023
As I don't have your data, I'm using random data -
LastName = {'Sanchez';'Johnson';'Li';'Diaz';'Brown'};
Age = [38;43;38;40;49];
Smoker = logical([1;0;1;0;1]);
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
BloodPressure = [124 93; 109 77; 125 83; 117 75; 122 80];
T = table(LastName,Age,Smoker,Height,Weight,BloodPressure)
T = 5×6 table
LastName Age Smoker Height Weight BloodPressure ___________ ___ ______ ______ ______ _____________ {'Sanchez'} 38 true 71 176 124 93 {'Johnson'} 43 false 69 163 109 77 {'Li' } 38 true 64 131 125 83 {'Diaz' } 40 false 67 133 117 75 {'Brown' } 49 true 64 119 122 80
> Using parenthesis with Column name yields a sub table -
T(:,"Weight")
ans = 5×1 table
Weight ______ 176 163 131 133 119
As you operate on a table, the output you will also be a table.
> Solution - You can use curly brackets
T.BMI = T{:,"Weight"}./T{:,"Height"}.^2
T = 5×7 table
LastName Age Smoker Height Weight BloodPressure BMI ___________ ___ ______ ______ ______ _____________ ________ {'Sanchez'} 38 true 71 176 124 93 0.034914 {'Johnson'} 43 false 69 163 109 77 0.034237 {'Li' } 38 true 64 131 125 83 0.031982 {'Diaz' } 40 false 67 133 117 75 0.029628 {'Brown' } 49 true 64 119 122 80 0.029053
Or dot notation
T.BMI = T.("Weight")./T.("Height").^2
T = 5×7 table
LastName Age Smoker Height Weight BloodPressure BMI ___________ ___ ______ ______ ______ _____________ ________ {'Sanchez'} 38 true 71 176 124 93 0.034914 {'Johnson'} 43 false 69 163 109 77 0.034237 {'Li' } 38 true 64 131 125 83 0.031982 {'Diaz' } 40 false 67 133 117 75 0.029628 {'Brown' } 49 true 64 119 122 80 0.029053
Read more here - Access data in a table

More Answers (0)

Categories

Find more on Tables in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!