What would be the function to create this new table ?

Hello there, my question is the following :
I have a table (let's say 20x2). I have duplicates on the first column but not on the second (for example the first 10 lines are x (each of these x have a their own value on the second column) and the 10 last lines are y (same logic).
With this function : Table2_grouped = varfun(@sum,Table2_bis,'InputVariables','second column','GroupingVariables','first column') I could regroup and cancel the duplicates and sum their own values. (I now have a 2x2 tables with x and y and their sums)
But here's my issue : i want now to create a new table where i once again have duplicates (so the 20 lines) but i want to keep the new value (meaning i want to keep to sum of the ten x and ten y). Hopefully this is clear and you can help me. thanks a lot

 采纳的回答

Do you want to repeat the new value ten times on the second column of the new table?
sumrepArray = [repmat(sumTable{1,2},10,1); repmat(sumTable{2,2},10,1)]
newTable = table(oldTable{:,1},sumrepArray);
but I'd recommend getting the size of each repeating elements from the oldtable and use this count to create the sumrep array.

4 个评论

Maybe my example was not the best one indeed. My original table has more than 2 duplicated variables which have their own values. Once i grouped these variables and get the sum I just want to go back to my original table and have as a second column the sum corresponding to the variable (second column will contain duplicates obviously). So yes to answer your question, i would like to repeat ten times the new value (so i can work on it). Could you please give me more details on " * but I'd recommend getting the size of each repeating elements from the oldtable and use this count to create the sumrep array."* thanks a lot
Aha, it's not so hard but I cannot test my code at the moment. Try something like the following (k is the 1 to number of repeating variables),
oldTable{oldTable{:,1}==sumTable{k,1}} = sumTable{k,2};
thanks for the answers ! :)
You're welcome. If it solved your problem, you could also accept the answer:)

请先登录,再进行评论。

更多回答(0 个)

类别

在 帮助中心 和 File Exchange 中查找有关 Get Started with MATLAB 的更多信息

产品

标签

Community Treasure Hunt

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

Start Hunting!

Translated by