How can I use unique on two separate matrices?

4 views (last 30 days)
dunlop11
dunlop11 on 29 Nov 2015
Answered: dpb on 29 Nov 2015
I have two matrices; A (2300x57) and ALabels (2300x1). I am using unique() and histc() to count the number of occurrences of certain values in A.
for j = 1:57
values = unique(A(:,j);
count = histc(A(:,j),values);
end
This works as expected, it adds up the occurrences of certain values in A. However, suppose A can have values 0-2, and ALabels can have values 0-1. Is there a way to calculate the number of occurrences of certain values in two different matrices? So on a smaller scale, consider these two matrices:
A = [0 1 2 0 1 2; B = [0;
1 2 0 1 2 1; 1;
0 1 0 1 2 2]; 1];
How can you calculate how many times '2' occurs in A AND with a corresponding '1' in the same row in B and store each calculation per column in A? Apologies if that wasn't clear, I am having trouble wording it. I would like this to return some sort of matrix that displays (for previous example):
For each column in A
number of times (A = 0 and B = 0);
number of times (A = 1 and B = 0);
number of times (A = 2 and B = 0);
number of times (A = 0 and B = 1);
number of times (A = 1 and B = 1);
number of times (A = 2 and B = 1);
I would like to use unique() and histc() again, just using two matrices at once. If there isn't a way to do this with unique(), is there another way?

Answers (1)

dpb
dpb on 29 Nov 2015
>> N=hist3([A(:,1) B],c)
N =
1 1
0 1
0 0
>>
Have to iterate over each column

Categories

Find more on Matrices and Arrays 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!