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
Star Strider
2017년 12월 27일
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.
Nicholas Hayden
2017년 12월 27일
Star Strider
2017년 12월 27일
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.
Nicholas Hayden
2017년 12월 27일
편집: Nicholas Hayden
2017년 12월 27일
dpb
2017년 12월 28일
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.
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!