0
votes

I have a 128 x 100 matrix in matlab, where each column should be treated as a separate element. Lets call this matrix M.

I have another 128 x 2000 matrix(called V) composed of columns from matrix M.

How would I make a histogram that maps the frequency of each column being used in the second matrix?

hist(double(V),double(M)) gives the error:

 Error using histc
Edge vector must be monotonically
non-decreasing.

what should I be doing?

2

2 Answers

0
votes

Here is an example. We start with data that resembles what you described

%# a matrix of 100 columns
M = rand(128,100);
sz = size(M);

%# a matrix composed of randomly selected columns of M (with replacement)
V = M(:,randi([1 sz(2)],[1 2000]));

Then:

%# map the columns to indices starting at 1
[~,~,idx] = unique([M,V]', 'rows', 'stable');
idx = idx(sz(2)+1:end);

%# count how many times each column occurs
count = histc(idx, 1:sz(2));

%# plot histogram
bar(1:sz(2), count, 'histc')
xlabel('column index'), ylabel('frequency')
set(gca, 'XLim',[1 sz(2)])

enter image description here

0
votes

[Lia,Locb] = ismember(A,B,'rows') also returns a vector, Locb, containing the highest index in B for each row in A that is also a row in B. The output vector, Locb, contains 0 wherever A is not a row of B.

ismember with the rows argument can identify which row of one matrix the rows of another matrix come from. Since it works on rows, and you are looking for columns, just transpose both matrices.

[~,Locb]=ismember(V',M');
histc(Locb)