How do I make a function to make calculations within my matrix?
古いコメントを表示

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.
採用された回答
その他の回答 (1 件)
Steven Lord
2020 年 11 月 2 日
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.
カテゴリ
ヘルプ センター および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!