Output of one table as a value into a new table

1 view (last 30 days)
I have a table, I call it TableA, with many rows and columns (each row represents a separate patient). I created a new column that defines to which group a patient belongs, consisting of 32 groups. This column is called 'group'.
I now want to calculate the amount of patients per group, and other demographic values such as average age (these are all known and part of TableA). I started with the amount of patiens per group, and I want to create a loop that generates the number of patients per group (for all 32 groups) and put the output into a new table, TableB.
I already created TableB which has 32 columns (one for each group), and several rows for all the information that I want to collect per group.
I tried to make a loop where I define the number of groups and want to index over each group, where the output is stored in the new TableB.
for i = 1:length(columnnames) % columnnames is the names that I gave to the columns in TableB, when I run the part length(columnames) it gives me 32 as it should
j = i + 1;
TableB(1,:) = sum(TableA.group(j));
end
I get the error 'Right hand side of an assignment into a table must be another table or a cell array.' and I don't precisely know how to fix this. TableA and TableB are both tables. Anybody that has a suggestion?
Thanks in advance!

Accepted Answer

VBBV
VBBV on 17 Nov 2022
TableB.group(j-1,:) = sum(TableA.group(j));
As the error states use table as above instead of array

More Answers (0)

Categories

Find more on Tables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!