MATLAB Answers

How do I make a function to make calculations within my matrix?

조회 수: 1(최근 30일)
Joost Lanjouw
Joost Lanjouw 2 Nov 2020
답변: Steven Lord 2 Nov 2020
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.

채택된 답변

Hiro 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.

추가 답변(1개)

Steven Lord
Steven Lord 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.
In the table array tableArray, you can index using names.
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.

Community Treasure Hunt

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

Start Hunting!

Translated by