How would you perform inter-row operations based on multiple columns?

I am a novice programmer that is primarily self-taught. I am new to MATLAB and relational mathematics. Currently, I am attempting to perform math operations between rows. I would like to normalize the exp by the corresponding con and then multiply by the constant.
Note: This constant is a laboratory measurement that could be subject to change in future experiments. Thus, I have given it a column.
Below is some sample code that I have generated to exemplify my problem and solution. I am trying to get from myTable to rTable.
I recognize my solution is very sloppy and there must be a way to perform these operations that is human-readable and uses less temporary variables. To put it shortly, there must be a simpler way.
rTable = table();
myTable = table(transpose(1:8), ...
transpose({'Con1', 'Con2', 'Exp1', 'Exp2',...
'Con1', 'Con2', 'Exp1', 'Exp2'}),...
transpose({'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'}),...
ones(8, 1) * 2,...
'VariableNames', {'Values' , 'Condition', 'Group', 'Constant'});
[r, c] = size(myTable)
a = myTable(strcmp(myTable.Group, 'A'), :);
b = myTable(strcmp(myTable.Group, 'B'), :);
aexp1 = a.Values(strcmp(a.Condition, 'Exp1'), :) / a.Values(strcmp(a.Condition, 'Con1'), :) * mean(a.Constant);
aexp2 = a.Values(strcmp(a.Condition, 'Exp2'), :) / a.Values(strcmp(a.Condition, 'Con2'), :) * mean(a.Constant);
bexp1 = b.Values(strcmp(b.Condition, 'Exp1'), :) / b.Values(strcmp(b.Condition, 'Con1'), :) * mean(b.Constant);
bexp2 = b.Values(strcmp(b.Condition, 'Exp2'), :) / b.Values(strcmp(b.Condition, 'Con2'), :) * mean(b.Constant);
aT = table(transpose({aexp1, aexp2}),...
transpose({'Exp1', 'Exp2'}),...
transpose({'A', 'A'}),...
transpose({2, 2,}),...
'VariableNames', {'Values', 'Condition', 'Group', 'Constant'});
bT = table(transpose({bexp1, bexp2}),...
transpose({'Exp1', 'Exp2'}),...
transpose({'B', 'B'}),...
transpose({2, 2,}),...
'VariableNames', {'Values', 'Condition', 'Group', 'Constant'});
rTable = [aT; bT]
Thank you for any input or suggestions. Perhaps, the data structure I am handling is poorly organized.
----
EDIT1 These are the completed variables of myTable and rTable
myTable
rTable
EDIT2:
Below is the code to create myTable and to create rTable. The myTable creation has been copied from the source code. The rTable creation has been modified such that no processes are needed to achieve this result.
myTable = table(transpose(1:8), ...
transpose({'Con1', 'Con2', 'Exp1', 'Exp2',...
'Con1', 'Con2', 'Exp1', 'Exp2'}),...
transpose({'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'}),...
ones(8, 1) * 2,...
'VariableNames', {'Values' , 'Condition', 'Group', 'Constant'});
The following should be the code for rTable. I have modified it so there is no need for the processes to reconstruct it.
rTable = table(transpose({6, 4, 2.8, 2.667}),...
transpose({'Exp1', 'Exp2', 'Exp1', 'Exp2'}),...
transpose({'A', 'A', 'B', 'B'}),...
transpose({2, 2, 2, 2}),...
'VariableNames', {'Values', 'Condition', 'Group', 'Constant'});

5 Comments

It would help to know what you are starting with, and what you want the result to be. Neither are obvious from your post.
If you believe that it would help us solve your problem, upload your data (or a representative sample of it) and attach it as a .mat, .txt, or .xlsx file to your original post.
Howdy! The variable assignment of `myTable` should be a representative format of the data I am working with. If my above code is executed. The current output of rTable should be the output that I am trying to reach. I will attach those tables from what I have gotten on me end.
A picture may be worth a thousand words, but the actual data allow us to experiment with them.
We prefer not having to reconstruct data by typing it in ourselves. We don’t always do it correctly. If we have your actual data, that eliminates at least one problem for us.
The code itself should be able to reconstruct the data. I will place it here.
myTable = table(transpose(1:8), ...
transpose({'Con1', 'Con2', 'Exp1', 'Exp2',...
'Con1', 'Con2', 'Exp1', 'Exp2'}),...
transpose({'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'}),...
ones(8, 1) * 2,...
'VariableNames', {'Values' , 'Condition', 'Group', 'Constant'});
The following should be the code for rTable. I have modified it so there is no need for the processes to reconstruct it.
rTable = table(transpose({6, 4, 2.8, 2.667}),...
transpose({'Exp1', 'Exp2', 'Exp1', 'Exp2'}),...
transpose({'A', 'A', 'B', 'B'}),...
transpose({2, 2, 2, 2}),...
'VariableNames', {'Values', 'Condition', 'Group', 'Constant'});
I will place this also in the edit.
Looks like a good place to try the splitapply function with findgroups but it's new enough I'm going to have to 'splore some and right now have to go clean up and head to town for a funeral--will try to look at some more later on.

Sign in to comment.

Answers (0)

Products

Tags

Asked:

on 27 Dec 2017

Commented:

dpb
on 28 Dec 2017

Community Treasure Hunt

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

Start Hunting!