Counting repeated values paired with other repeated values and placing those counts in an array

7 次查看(过去 30 天)
I have a dataset in which different stimulus levels are presented multiple times. So far I've managed to boil this down using unique and arrayfun to produce one column of unique values and a second column stating how many times the unique value was presented during the experiment. Here's my code for that, which I obtained through looking in MATLAB's FAQs regarding unique:
if true
myfastrepeat1(:,1) = unique(myfasttotal1(:,1));
myfastrepeattemp = arrayfun(@(x)length(find(myfasttotal1(:,1) == x)), unique(myfasttotal1(:,1)), 'Uniform', false);
myfastrepeat1(:,2) = cell2mat(myfastrepeattemp);
end
So say I have three incidences of stimulus -0.5768 - of those three presentations, the observer got two presentations correct. In my myfasttotal1 file, this would be represented thus:
[-0.5768, 0; -0.5768, 1; -0.5768, 1]
Having got this far, I'm now a bit stuck as to how to tackle creating a new column in myfastrepeat1 which states how many presentations were correct for each unique stimulus. I'm not sure whether I should still be using arrayfun for this or using histc, or indeed going down the indexing route. I figure that provided I can get MATLAB to consider each set of repeated stimulus values as a group, I could then get the count of 1 for each group, so in the above example the count of 1s would be 2, representing the number of correct answers.
Thankyou in advance for any advice you can give, and if you need more elaboration on what I'm trying to do, let me know!
  5 个评论
Marianne
Marianne 2012-8-16
Hi, I actually fixed it using some while loops on advice of my boyfriend who knows Java. Here's my code, but here's a sample dataset as well in case anyone else wants to have a go!
Code:
if true
k = 1; % k cannot be 0 due to using to find row number of a value in a column.
while k <= m;
% ensure k does not exceed array size and trigger loop repeat.
currentvalue = myfastrepeat1(k);
% gets the first unique stimulus value in the column.
numcorrect = 0;
l = 1;
while l <= 120;
% ensure l does not exceed array size of myfasttotal and trigger loop repeat.
if myfasttotal1(l) == currentvalue;
if myfasttotal1(l,2) == 1;
numcorrect = (numcorrect + 1);
% checks the numbered row in myfasttotal to see if stimulus matches with numbered row in myfastrepeat, then looks to see if there is a 1 in the column next to it indicating correctly identified.
myfastrepeat1(k,3) = numcorrect;
% if correct, increase numcorrect by 1 each time and put in corresponding row in myfastrepeat.
end
end
l = l + 1;
% increase l to go and look at next row in myfasttotal. Means whole column is checked before next instance of k.
end
k = k + 1;
% increase k to get next unique stimulus number from myfastrepeat.
end
Sample array:
myfasttotal1 =
-0.7696 0
-0.7447 1.0000
-0.7447 1.0000
-0.7212 1.0000
-0.7212 1.0000
-0.7212 1.0000
-0.6990 1.0000
-0.6990 1.0000
-0.6990 1.0000
-0.6778 0
-0.6576 0
-0.6576 1.0000
-0.6576 1.0000
-0.6383 0
-0.6383 1.0000
-0.6383 1.0000
-0.6383 1.0000
-0.6383 1.0000
-0.6198 1.0000
-0.6198 1.0000
-0.6198 1.0000
-0.6198 1.0000
-0.6021 1.0000
-0.6021 1.0000
-0.5850 0
-0.5850 1.0000
-0.5686 1.0000
-0.5686 1.0000
-0.5686 1.0000
-0.5528 0
-0.5528 0
-0.5528 1.0000
-0.5376 0
-0.5376 0
-0.5376 0
-0.5376 0
-0.5376 0
-0.5376 0
-0.5376 1.0000
-0.5376 1.0000
-0.5376 1.0000
-0.5376 1.0000
-0.5376 1.0000
-0.5229 0
-0.5229 0
-0.5229 1.0000
-0.5229 1.0000
-0.5229 1.0000
-0.5229 1.0000
-0.5229 1.0000
-0.5086 0
-0.5086 1.0000
-0.5086 1.0000
-0.4949 1.0000
-0.4815 0
-0.4815 1.0000
-0.4815 1.0000
-0.4685 0
-0.4685 0
-0.4685 1.0000
-0.4685 1.0000
-0.4685 1.0000
-0.4559 0
-0.4559 1.0000
-0.4437 1.0000
-0.4437 1.0000
-0.4318 1.0000
-0.4318 1.0000
-0.4202 0
-0.4202 0
-0.4202 1.0000
-0.4089 0
-0.4089 1.0000
-0.3979 0
-0.3979 1.0000
-0.3872 0
-0.3872 1.0000
-0.3665 0
-0.3565 1.0000
-0.3565 1.0000
-0.3468 1.0000
-0.3468 1.0000
-0.3468 1.0000
-0.3372 0
-0.3279 1.0000
-0.3279 1.0000
-0.3188 1.0000
-0.3188 1.0000
-0.3098 0
-0.3098 1.0000
-0.3098 1.0000
-0.3098 1.0000
-0.2924 0
-0.2840 0
-0.2757 1.0000
-0.2676 0
-0.2676 1.0000
-0.2596 1.0000
-0.2596 1.0000
-0.2596 1.0000
-0.2518 1.0000
-0.2441 0
-0.2366 0
-0.2366 1.0000
-0.2366 1.0000
-0.2366 1.0000
-0.2291 0
-0.2218 0
-0.2147 1.0000
-0.2076 1.0000
-0.2007 0
-0.2007 1.0000
-0.1938 1.0000
-0.1871 1.0000
-0.1805 1.0000
-0.1739 1.0000
-0.1675 1.0000
-0.1675 1.0000
-0.1675 1.0000
-0.1549 1.0000
end

请先登录,再进行评论。

采纳的回答

Oleg Komarov
Oleg Komarov 2012-8-16
[un,trash,sub] = unique(myfasttotal1(:,1));
count = accumarray(sub,myfasttotal1(:,2));
[un, count]
The last line will display for each stimulus level, the number of successes.
  2 个评论
Marianne
Marianne 2012-8-16
Excellent! I did come across accumarray a few times while browsing the questions on here but wasn't sure if it could separate while accumulating. I shall go away and read the help files on it to make sure I understand what's going on with it, and then I'll change my code. Thankyou!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Entering Commands 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by