remove non matching elements from matrix - matrix

I am trying to compare two matrices A and B. If elements in the first two columns of A match those in B, I want to delete all non matching rows from A. The third column in B should not factor into the comparison.
A = [1 2 3 B = [1 2 8
3 4 5 3 4 5]
6 7 8]
Desired result:
A = [1 2 3
3 4 5]
So far I only found ways to remove duplicate entries, which is the exact opposite of what I want. How can I do this?

You can efficiently use ismember for this task:
% Input matrices
A = [1 2 3; 3 4 5; 7 8 9];
B = [1 2 8; 3 4 5];
A1 = A(:,1:2); % Extract first two columns for both matrices
B1 = B(:,1:2);
[~,ii] = ismember(A1,B1,'rows'); % Returns which rows in A1 are also in B1
ii = ii(ii>0); % Where ii is zero, it's a non-matching row
A(ii,:) % Index to keep only matching rows
All of this can be written more compactly, but I wanted to show the step-by-step process first:
[~,ii] = ismember(A(:,1:2),B(:,1:2),'rows');
A(ii(ii>0),:)

A = [1 2 3;3 4 5;7 8 9];
B = [1 2 8; 3 4 5];
tmp = min([size(A,1) size(B,1)]); % get size to loop over
k = false(tmp,1); % storage counter
for ii = 1:tmp
if all(A(ii,1:2)==B(ii,1:2)) % if the first two columns match
k(ii)=true; % store
end
end
C = A(k,:) % extract requested rows

Related

Loop over part of Matrix

I have a Matrix A. I want to iterate over the inner part of the matrix (B), while also working with the rows and columns that are not part of B.
A = [1 4 5 6 7 1; B = [2 2 2 2;
8 2 2 2 2 1; 2 3 3 2;
9 2 3 3 2 1; 2 8 2 2];
0 2 8 2 2 1;
1 1 1 1 1 1];
I know it is possible to select the part of A like this:
[rows,columns] = size(A);
B = A([2:1:rows-1],[2:1:columns-1]);
for i = 1:(rows*columns)
%do loop stuff
endfor
This however wont work because I also need the outer rows and columns for my calculations. How can I achieve a loop without altering A?
So, why do not use two indexes for the inner matrix?
%....
for i=2:rows-1
for j=2:cols-1
% here, A(i,j) are the B elements, but you
% can still access to A(i-1, j+1) if you want.
end
end
%....

How to change the elements by different values in a matrix simultaneously in Octave?

I want to change the individual elements in a matrix by different values simultaneously.
How do I do that?
For example: I want to change the first element in matrix A by certain amount and the second element by a different amount simultaneously.
{ A = [1; 2]
% instead of doing A(1) = .....
A(2) = .....
}
You can access the elements of a vector or matrix and replace them.
For a vector this is intuitive.
octave:16> A = 1:9
A =
1 2 3 4 5 6 7 8 9
octave:17> A([1 3 5 7 9]) = 0
A =
0 2 0 4 0 6 0 8 0
This can be done for a matrix as well. The elements of a matrix are arranged in a column-first manner. You can use a single index to access the elements of a matrix.
octave:18> A = [1 2 3; 4 5 6; 7 8 9]
A =
1 2 3
4 5 6
7 8 9
The 2nd element of A is the same as A(2, 1). The 4th element of A is the same as A(1, 2).
octave:21> A(2)
ans = 4
octave:22> A(4)
ans = 2
So, you can set all the odd elements of A to 0 in one go like this:
octave:19> A([1 3 5 7 9]) = 0
A =
0 2 0
4 0 6
0 8 0
Just add a vector with the differences. A += [0.1; 0.2]
octave:1> A = [1; 2];
octave:2> A += [0.1; 0.2]
A =
1.1000
2.2000

Matlab: sorting a matrix in a unique way

I have a problem with sorting some finance data based on firmnumbers. So given is a matrix that looks like:
[1 3 4 7;
1 2 7 8;
2 3 7 8;]
On Matlab i would like the matrix to be sorted as follows:
[1 0 3 4 7 0;
1 2 0 0 7 8;
0 2 3 0 7 8;]
So basically every column needs to consist of 1 type of number.
I have tried many things but i cant get the matrix sorted properly.
A = [1 3 4 7;
1 2 7 8;
2 3 7 8;]
%// Get a unique list of numbers in the order that you want them to appear as the new columns
U = unique(A(:))'
%'//For each column (of your output, same as columns of U), find which rows have that number. Do this by making A 3D so that bsxfun compares each element with each element
temp1 = bsxfun(#eq,permute(A,[1,3,2]),U)
%// Consolidate this into a boolean matrix with the right dimensions and 1 where you'll have a number in your final answer
temp2 = any(temp1,3)
%// Finally multiply each line with U
bsxfun(#times, temp2, U)
So you can do that all in one line but I broke it up to make it easier to understand. I suggest you run each line and look at the output to see how it works. It might seem complicated but it's worthwhile getting to understand bsxfun as it's a really useful function. The first use which also uses permute is a bit more tricky so I suggest you first make sure you understand that last line and then work backwards.
What you are asking can also be seen as an histogram
A = [1 3 4 7;
1 2 7 8;
2 3 7 8;]
uniquevalues = unique(A(:))
N = histc(A,uniquevalues' ,2) %//'
B = bsxfun(#times,N,uniquevalues') %//'
%// bsxfun can replace the following instructions:
%//(the instructions are equivalent only when each value appears only once per row )
%// B = repmat(uniquevalues', size(A,1),1)
%// B(N==0) = 0
Answer without assumptions - Simplified
I did not feel comfortable with my old answer that makes the assumption of everything being an integer and removed the possibility of duplicates, so I came up with a different solution based on #lib's suggestion of using a histogram and counting method.
The only case I can see this not working for is if a 0 is entered. you will end up with a column of all zeros, which one might interpret as all rows initially containing a zero, but that would be incorrect. you could uses nan instead of zeros in that case, but not sure what this data is being put into, and if it that processing would freak out.
EDITED
Includes sorting of secondary matrix, B, along with A.
A = [-1 3 4 7 9; 0 2 2 7 8.2; 2 3 5 9 8];
B = [5 4 3 2 1; 1 2 3 4 5; 10 9 8 7 6];
keys = unique(A);
[counts,bin] = histc(A,transpose(unique(A)),2);
A_sorted = cell(size(A,1),1);
for ii = 1:size(A,1)
for jj = 1:numel(keys)
temp = zeros(1,max(counts(:,jj)));
temp(1:counts(ii,jj)) = keys(jj);
A_sorted{ii} = [A_sorted{ii},temp];
end
end
A_sorted = cell2mat(A_sorted);
B_sorted = nan(size(A_sorted));
for ii = 1:size(bin,1)
for jj = 1:size(bin,2)
idx = bin(ii,jj);
while ~isnan(B_sorted(ii,idx))
idx = idx+1;
end
B_sorted(ii,idx) = B(ii,jj);
end
end
B_sorted(isnan(B_sorted)) = 0
You can create at the beginning a matrix with 9 columns , and treat the values in your original matrix as column indexes.
A = [1 3 4 7;
1 2 7 8;
2 3 7 8;]
B = zeros(3,max(A(:)))
for i = 1:size(A,1)
B(i,A(i,:)) = A(i,:)
end
B(:,~any(B,1)) = []

Replacing elements of a 2D matrix

I'm trying to increase the efficiency of my MATLAB code. What it does is, it replaces the nonzero elements of a matrix with the multiplication of the rest of the nonzero elements in the same row. For instance,
X = [2 3 6 0; 0 3 4 2]
transforms into
X = [18 12 6 0; 0 8 6 12]
It's an easy task to implement within a for loop. It checks every row, finds nonzero values and do its replacements. I want to get rid of the for loop though. Is there a way to implement this without a loop?
Code
X = [2 3 6 0; 0 3 4 2];
X1 = X;
X1(~X) = 1;
out = bsxfun(#rdivide,prod(X1,2),X1).*(X~=0)
Output
out =
18 12 6 0
0 8 6 12
Probably getting the row product first once and then divide by the element you don't want is the simplest way:
X = [2 3 6 0; 0 3 4 2]
Y=X
%get the product of all elements in a row
Y(Y==0)=1
Y=prod(Y,2)
%repeat Y to match the size of X
Y=repmat(Y,1,size(X,2))
%For all but the zero elements, divide Y by X, which is the product of all other elements.
X(X~=0)=Y(X~=0)./X(X~=0)

Filter some rows from a matrix

Suppose I have this matrix:
matrix = [2 2; 2 3; 3 4; 4 5]
And now I'd like to filter out all rows which do not begin with an even number to produce
[2 2; 2 3; 4 5]
Is there a high-level procedure for doing this, or do I have to code for it?
You can get a logical index for the rows whose first element is even, and use : to select all the columns. Here's how it's done, line by line:
octave> matrix = [2 2; 2 3; 3 4; 4 5]
matrix =
2 2
2 3
3 4
4 5
octave> ! mod (matrix(:,1), 2)
ans =
1
1
0
1
octave> matrix(! mod (matrix(:,1), 2),:)
ans =
2 2
2 3
4 5
EDIT: in the comments below it was asked for other selection methods. I'm unaware of any specific function for it, but the thing above is indexing with a function:
even_rows = matrix(! mod (matrix(:,1), 2), :) # first element is even
s3_rows = matrix(matrix(:,1) == 3, :); # first element is 3
int_rows = matrix(fix (matrix(:,1)), == matrix(:,1), :); # first element is an integer
IF there was a function, one would still have to write the function, it wouldn't be any easier shorter or easier to read. But if you want to write a function, you could:
function selec = select_rows (func, mt)
selec = mt(func (mt(:,1)),:);
endfunction
even_rows = select_rows (#(x) ! mod (x, 2), matrix);
se_rows = select_rows (#(x) x == 3, matrix);
int_rows = select_rows (#(x) fix (x) == x, matrix);
EDIT2: to have the rows that have already matched, simply keep track of them on the mask. Example:
mask = ! mod (matrix(:,1), 2); # mask for even numbers
even = matrix(mask,:);
mask = ! mask & matrix(:,1) == 3; # mask for left overs starting with a 3
s3 = matrix(mask,:);
rest = matrix(! mask, :); # get the leftovers
As above, you could write a function that does it. It would take a matrix as the first argument plus any number of function handles. It would iterate over the function handles modifying the mask everytime and filling a cell array with the matrices.

Resources