How to subtract rows when there is a value repeated more than once ?
2 views (last 30 days)
Show older comments
Benjamin Dupre
on 25 Jun 2018
Commented: Benjamin Dupre
on 25 Jun 2018
I want to fill the interval item (Int_item) variable with the difference between the present trial (trial) and the past trial where the observation (obs) appeared. Leave it with a zero for those observations that are not repeated (appereance_number [ap_n] >1).
a =
270×9 table
onset ap_n obs response eval rtim trial class Int_item
_______________ ____ ___ ________ ____ ____ _____ _____ ________
"_10:50:57.315" 1 1 1 1 886 1 0 0
"_10:51:21.293" 2 1 0 0 0 7 0 0
"_10:53:57.247" 3 1 4 1 825 46 0 0
"_10:51:01.313" 1 2 1 1 733 2 0 0
"_10:51:29.303" 2 2 4 1 1478 9 0 0
"_10:54:09.242" 3 2 4 1 948 49 0 0
"_10:51:05.311" 1 3 1 1 929 3 0 0
"_10:51:33.301" 2 3 4 1 904 10 0 0
"_10:54:25.237" 3 3 4 1 1687 53 0 0
"_10:51:09.310" 1 4 1 1 866 4 0 0
"_10:51:37.300" 2 4 4 1 758 11 0 0
"_10:54:33.234" 3 4 4 1 740 55 0 0
"_10:51:13.309" 1 5 1 1 1028 5 0 0
"_10:51:45.296" 2 5 4 1 1283 13 0 0
"_10:54:45.230" 3 5 4 1 811 58 0 0
It aiming to end up with something similar to this in the Int_item column.
a =
270×9 table
onset ap_n obs response eval rtim trial class Int_item
_______________ ____ ___ ________ ____ ____ _____ _____ ________
"_10:50:57.315" 1 1 1 1 886 1 0 0
"_10:51:21.293" 2 1 0 0 0 7 0 6
"_10:53:57.247" 3 1 4 1 825 46 0 39
"_10:51:01.313" 1 2 1 1 733 2 0 0
"_10:51:29.303" 2 2 4 1 1478 9 0 7
"_10:54:09.242" 3 2 4 1 948 49 0 40
"_10:51:05.311" 1 3 1 1 929 3 0 0
"_10:51:33.301" 2 3 4 1 904 10 0 7
"_10:54:25.237" 3 3 4 1 1687 53 0 43
"_10:51:09.310" 1 4 1 1 866 4 0 0
"_10:51:37.300" 2 4 4 1 758 11 0 7
"_10:54:33.234" 3 4 4 1 740 55 0 44
"_10:51:13.309" 1 5 1 1 1028 5 0 0
"_10:51:45.296" 2 5 4 1 1283 13 0 8
"_10:54:45.230" 3 5 4 1 811 58 0 43
This is the code from what I'm trying:
a = DATA{1,1}; % CREAT TEMP VAR
a.class = zeros(270,1); % add a new collumn
a.Int_item = zeros(270,1); % add a new collumn
a = sortrows(a,[3 2]); % making it easy to find interval of items between rep
for i = 2:height(a)
a.Int_item = a.trial(i)-a.trial(i-1)
end
To assign to or create a variable in a table, the number of rows must match the height of the table.
I hope the question is clear.
All best,
0 Comments
Accepted Answer
Ankita Bansal
on 25 Jun 2018
Edited: Ankita Bansal
on 25 Jun 2018
Hi, there are some errors in for loop. The size of a.Int_item is 270x1 while that of a.trial(i)-a.trial(i-1) is 1x1. Also, you have not inserted the condition of ap_n>1.
for i = 2:height(a)
if a.ap_n(i)>1
a.Int_item(i) = a.trial(i)-a.trial(i-1);
end
end
More Answers (0)
See Also
Categories
Find more on Migrate GUIDE Apps 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!