finding the frequency of each row (3) - matlab

Per my previous question couple days ago, now, I have several mx3 matrices with rows from (0,1,num), (-1,0,num), (0,1,num), (0,-1,num), (1,1,num), (-1,1,num), (1,-1,num),(-1,-1,num), where num is an integer which can take any values between 0 to 3.
I would like to create a new matrix, with 8 rows, and 6 columns, where the the first two columns represent each of the above coordinates, and each of the remaining columns indicate the frequency
of each of the above coordinates at each num values. i.e. columns 3 of each row indicates the number of times we see the coordinate corresponding to that row with and num=0. columns 4 of each row indicates the number of times we see the coordinate corresponding to that row with and num=1.
columns 5 of each row indicates the number of times we see the coordinate corresponding to that row with and num=2, and columns 6 of each row indicates the number of times we see the coordinate corresponding to that row with and num=3.
For instance, if A=[0 1 1
1 1 1
1 1 0
1 0 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 -1 0
1 1 0
1 1 3
1 1 2
1 1 3
1 1 3]
I would like to see something like:
-1 -1 0 0 0 0
-1 0 0 0 0 0
-1 1 0 0 0 0
0 -1 0 0 0 0
0 1 0 1 0 0
1 -1 1 0 0 0
1 0 1 0 0 0
1 1 7 1 1 3
Is there a way to do it? Thanks.

Try this:
counts = zeros(9, 6); % Initialize output matrix
k = 1;
for ii = -1:1
for jj = -1:1
ijCoords = (A(:,1) == ii) & (A(:,2) == jj); % Find rows containing coordinate (ii,jj)
ijCount = histc(A(ijCoords,3), 0:3); % Count how many 0,1,2,3 in these rows
counts(k,:) = [ii, jj, ijCount(:)']; % Add the counts to the next row of the output matrix
k = k + 1;
end
end
counts(5, :) = []; % Remove coordinate (0,0) because you don't want it.

Related

Is there a fast way to count occurrences of items in a matrix and save them in another matrix without using loops?

I have a time-series matrix X whose first column contains user ID and second column contains the item ID they used at different times:
X=[1 4
2 1
4 2
2 3
3 4
1 1
4 2
5 3
2 1
4 2
5 4];
I want to find out which user used which item how many times, and save it in a matrix Y. The rows of Y represent users in ascending order of ID, and the columns represent items in ascending order of ID:
Y=[1 0 0 1
2 0 1 0
0 0 0 1
0 3 0 0
0 0 1 1]
The code I use to find matrix Y uses 2 for loops which is unwieldy for my large data:
no_of_users = size(unique(X(:,1)),1);
no_of_items = size(unique(X(:,2)),1);
users=unique(X(:,1));
Y=zeros(no_of_users,no_of_items);
for a=1:size(A,1)
for b=1:no_of_users
if X(a,1)==users(b,1)
Y(b,X(a,2)) = Y(b,X(a,2)) + 1;
end
end
end
Is there a more time efficient way to do it?
sparse creates a sparse matrix from row/column indices, conveniently accumulating the number of occurrences if you give a scalar value of 1. Just convert to a full matrix.
Y = full(sparse(X(:,1), X(:,2), 1))
Y =
1 0 0 1
2 0 1 0
0 0 0 1
0 3 0 0
0 0 1 1
But it's probably quicker to just use accumarray as suggested in the comments:
>> Y2 = accumarray(X, 1)
Y2 =
1 0 0 1
2 0 1 0
0 0 0 1
0 3 0 0
0 0 1 1
(In Octave, sparse seems to take about 50% longer than accumarray.)

Putting 1's in certain places

I have 2 matrices
Matrix A = [7 3 5 2 8 4 1 6 9;
5 2 6 1 4 3 9 7 8;
9 1 4 5 2 6 3 6 7;
4 8 1 6 3 7 2 9 5;
6 1 7 2 8 4 5 9 3]
Matrix B = [1 0 0 0 0 0 0 0 0;
0 1 1 0 0 0 0 0 0;
0 0 0 0 0 0 0 1 0;
0 0 0 1 0 1 0 0 0;
0 0 0 0 0 0 0 0 1]
Matrix A and B are already defined.
Here each column can't have more than 1 what i want to do is that if when i do sum for Matrix B if i found 0 in it i have to add 1's in the places of the zero's but in certain places. In each row the 1's have to be placed in certain groups. For example if a 1 is placed in column 1, then it can be placed as well in column 2 or 3 only. It can't be placed anywhere else. If in another row it is placed in column 5, then it can be placed in column 4 or 6 only and so on. It's like group of 3. Each 3 columns are together.
To be more clear:
Here the sum of matrix B is [1 1 1 1 0 1 0 1 1]. The zeros here are placed in column 5 and 7 and i want to add 1 putting in mind where the 1 is going to be placed in the matrix. So in this example the 1 of column 5 can only be placed in row 4 as the 1's in this row are placed in column 4 and 6. The 1 of column 7 can be placed in row 5 or row 3. If we have choice between 2 rows then the 1 will be placed in the placed of the higher number of Matrix A.
The 1's have to be placed in groups; columns 1, 2 and 3 are together, columns 4,5 and 6 are together and columns 7, 8 and 9 are together. so if the 1 is placed in 1 column of the group then it can't be placed in any other place.
Let me simplify it if we have an array like this [0 0 0 0 0 0 0 1 1] This array has 3 categories, columns 1,2 and 3 are 1st category, columns 4,5 and 6 are 2nd category and so on. here i want to place a 1 so that the 3rd category won't have a zero element. This is what i want to do briefly but with a whole matrix with all the categories.
so here the output will be =
[1 0 0 0 0 0 0 0 0;
0 1 1 0 0 0 0 0 0;
0 0 0 0 0 0 0 1 0;
0 0 0 1 1 1 0 0 0;
0 0 0 0 0 0 1 0 1]
This code was tried but it doesn't give the required output as the 1 was placed in the 1st row not in the place where it has to be (the category that it should be in).
sum_cols_B = sum(B); % Sum of Matrix B (dim 1)
[~, idx] = find(sum_cols_B == 0); % Get indices where sum == 0
% Using loop to go through the indices (where sum = 0)
for ii = idx
B(1,ii) = 1; % Insert 1 in the first position of that
end % column in Matrix B
Ask me if the question is still not clear.!
Here's an updated loop that will add the missing 1's:
sum_cols_B = sum(B);
[~, idx] = find(sum_cols_B == 0);
group_size = 3;
for ii = idx
% Calculate the starting column of the group for column ii
% There are (ii-1)/group_size groups
% Add 1 for 1-based indexing
group_start = floor((ii-1)/group_size)*group_size + 1;
% Determine which rows in the current group have nonzero values
group_mask = sum(B(:,group_start:group_start+group_size-1), 2) > 0;
% Find the row number of the max in A column ii corresponding to mask
[~,rownum] = max(A(:,ii).*group_mask);
% The value in column ii of B should have a 1 inserted
% at the row containing the max in A
B(rownum,ii) = 1;
end
Results for B above are:
B =
1 0 0 0 0 0 0 0 0
0 1 1 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0
0 0 0 1 1 1 0 0 0
0 0 0 0 0 0 1 0 1
B = [1 0 0 0 0 0 0 0 0;...
0 1 1 0 0 0 0 0 0;...
0 0 0 0 0 0 0 1 0;...
0 0 0 1 0 1 0 0 0;...
0 0 0 0 0 0 0 0 1]; % Matrix - using this as an example
sum_cols_B = sum(B); % Sum of Matrix B (dim 1)
[~, idx] = find(sum_cols_B == 0); % Get indices where sum == 0
% Using loop to go through the indices (where sum = 0)
for ii = idx
B(1,ii) = 1; % Insert 1 in the first position of that
end % column in Matrix B

Select all BUT certain index pairs in multi-dimensional array Matlab

I am trying to select all BUT certain index pairs in a multi-dimensional array. i.e. I have a set of paired indices (e.g. [1,2] and [4,5]). I want to set all BUT those indexed pairs to 0.
The closest I have come to this is:
A(setdiff(1:length(A(:,1)),lon),setdiff(1:length(A(1,:)),lat)) = 0;
, where A is the matrix and lon and lat are the index pairs I want to keep. However, that also leaves all the intersecting rows and columns of those pairs.
Any ideas?
Here is some example code:
A = ones([5,5])
A =
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
lon = [1];
lat = [4];
A(setdiff(1:length(A(:,1)),lon),setdiff(1:length(A(1,:)),lat)) = 0
A =
1 1 1 1 1
0 0 0 1 0
0 0 0 1 0
0 0 0 1 0
0 0 0 1 0
What I want is:
A =
0 0 0 1 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
The easiest thing to do is actually the opposite of what you have tried. First you want to start with a matrix of zeros and then only fill in those pairs that you have stored in lat and lon. Also because you have paired subscripts, you will want to convert those to a linear index using sub2ind
%// Convert subscripts to a linear index
inds = sub2ind(size(A), lon, lat);
%// Start off with a matrix of zeros
B = zeros(size(A));
%// Fill in the values at the specified lat/lon from A
B(inds) = A(inds);

Matrix of 0s and 1s Where Assignment in Subsequent Rows are Contingent on the Previous Row

I'd like to create a Matrix in MATLAB where:
The First row consists of a random arrangement of 0s and 1s, split evenly (i.e. 50-50).
The Second row randomly assigns zeros to 50% of the 0s and 1s in the first row, and ones to the remaining 50%.
The Third row randomly assigns zeros to 50% of the 0s and 1s in the second row, and ones to the remaining 50%.
Non-randomized Example:
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1
0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1
Any suggestions?
A solution based on checking whether numbers are bigger or smaller than the median. As long as the number of columns tested is even, exactly half of a set of random doubles will be bigger than the median, and half will be smaller. This guarantees that there's exactly 50% of bits get flipped.
nRows = 3;
nCols = 16; %# divisible by 4
%# seed the array
%# assume that the numbers in each row are unique (very, very likely)
array = rand(nRows,nCols);
out = false(nRows,nCols);
%# first row is special
out(1,:) = array(1,:) > median(array(1,:));
%# for the rest of the row, check median for the zeros/ones in the previous row
for iRow = 2:nRows
zeroIdx = out(iRow-1,:) == 0;
%# > or < do not matter, both will replace zeros/ones
%# and replace with exactly half zeros and half ones
out(iRow,zeroIdx) = array(iRow,zeroIdx) > median(array(iRow,zeroIdx));
out(iRow,~zeroIdx) = array(iRow,~zeroIdx) > median(array(iRow,~zeroIdx));
end
I'd offer a short bsxfun solution:
%// number of divisions
n = 4;
%// unshuffled matrix like in your example
unshuffled = bsxfun(#(a,b) mod(a,2*b) > b-1, meshgrid(1:n^2,1:n) - 1, (2.^((n-1):-1:0)).') %'
%// shuffle columns
shuffled = unshuffled(:,randperm(n^2))
unshuffled =
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1
0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
shuffled =
1 0 1 1 0 1 0 1 1 1 1 0 0 0 0 0
1 1 1 0 0 1 1 0 1 0 0 0 1 0 1 0
1 0 0 1 0 0 0 0 1 1 0 1 1 0 1 1
1 1 1 1 0 0 0 0 0 0 1 0 0 1 1 1
First you need to create the unshuffled matrix, which can be done by comparing the matrix generated by meshgrid(1:n^2,1:n) with a row dependent modulus. Finally you just need to shuffle the columns.
If you have the Statistics Toolbox, you can do it very easily with randsample:
M = 3; %// number of rows
N = 16; %// number of columns. Should be multiple of 4, according to problem definition
result = zeros(M,N); %// preallocate and initiallize to zeros
result(1, randsample(1:N,N/2)) = 1; %// first row: half values set to one, half to zero
for m = 2:M
result(m, :) = result(m-1, :); %// initiallize row m equal to row m-1
result(m, randsample(find(result(m-1,:)), N/4)) = 0; %// change half of ones
result(m, randsample(find(~result(m-1,:)), N/4)) = 1; %// change half of zeros
end
Example result:
result =
0 1 0 1 1 0 0 0 0 1 0 1 1 0 1 1
1 1 0 0 0 1 1 1 0 1 0 1 0 0 0 1
1 0 0 0 1 0 0 1 0 1 1 0 1 1 0 1
A solution using randperm:
nrows = 3;
ncols = 16;
M = zeros(nrows,ncols);
%// seed the first row
M(1,1:ncols/2) = 1;
M(1,:) = M(1,randperm(ncols));
for r = 2:nrows
%// Find ncols/4 random between 1 and ncols/2. These will be used to index half of the previous rows 1 elements and set them to one
idx = randperm(ncols/2);
idx1 = idx(1:ncols/4);
%// Do the same thing again, but this time it will be used for the 0 elements of the previous row
idx = randperm(ncols/2);
idx0 = idx(1:ncols/4);
idx_prev1 = find(M(r-1,:)); %// Find where the 1 elements were in the last row
idx_prev0 = find(~M(r-1,:)); %// Find where the 0 elements were in the last row
M(r,idx_prev1(idx1))=1; %// Set half of the previous rows 1 elements in this row to 1
M(r,idx_prev0(idx0))=1; %// Set half of the previous rows 0 elements in this row to 1
end

is there a better way of assigning values to a matrix

say i have a matrix
A=zeros(10,3);
and a vector
ll=[1 1 1 2 2 2 3 1 3 2]';
and i want to assign the value in each row corresponding to the value in ll for that row to be 1
i.e output would be
A= 1 0 0
1 0 0
1 0 0
0 1 0
0 1 0
0 1 0
0 0 1
1 0 0
0 0 1
0 1 0
how i do it is using a for loop
for ii=1:length(ll)
A(ii,ll(ii)=1;
end
This should do the trick:
ll=[1 1 1 2 2 2 3 1 3 2]';
A=bsxfun(#eq,ll,1:max(ll))
I'm using bsxfun to check when the entry of ll is equal to an element of the row vector [1 2 3] (in this case). If the entry of ll is 1, it will be equal to the entry in the first column of the [1 2 3] vector and will give a 1 in the first column of A and zeros in the rest of the columns of that row.
Just convert to a linear index:
A((ll-1)*size(A,1) + (1:size(A,1)).') = 1;