How do I make a function to make calculations within my matrix?
1 view (last 30 days)
Show older comments
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/400464/image.png)
The columns in this matrix are the following: trial_number participant_number gab_ori1 gab_ori2 gab_ori3 gab_ori4 target response
How do I make a function that substracts the response column from the relevant gab_ori column? For example, if the target is 1 then I want to substract the response from gab_ori1. For the last row that would be 158 - 117.47 (of course the actual target in this row is 4, this is just an example). If the target were to be 3, then I want to substract the response from gab_ori3 etc.
I never worked with functions before so if anyone could help me in the right direction that would be greatly appreciated.
0 Comments
Accepted Answer
Hiro Yoshino
on 2 Nov 2020
It seems like you have limited experience with MATLAB before?
You should begin with MATLAB onramp (click here). I guess table would be a good fit for your application.
It's hard to have you understand by anyone - the best bet is build up your foundation with MATLAB first, and then come back here again if necessary. You will be able to make the question more specific and narrowed so that the experts can guide you with more ease.
0 Comments
More Answers (1)
Steven Lord
on 2 Nov 2020
How have you stored your data? A numeric matrix or a table array?
numericMatrix = magic(4)
tableArray = array2table(numericMatrix, 'VariableNames', ["x1", "x2", "x3", "x4"])
You'd have to manually keep track of which column in numericMatrix is x1, x2, x3, and x4 and index into that column.
n = 3;
variableName = "x" + n;
data = tableArray{:, variableName} % This is the contents of variable x3
Note that I used curly braces to index into tableArray to retrieve the contents of x3. If I'd used parentheses I'd have retrieved a smaller table, one that contained only the variable x3.
0 Comments
See Also
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!