How check if pair of values exist in 2d Matlab array? - matlab

Say I have
a=[1 2 3 4; 5 6 7 8];
If I then have x=3, y=7, how can I check that (3,7) exists in array a but also ensure that if I check for the pair x=3, y=8 (3,8), then it returns false and NOT true?
EDIT: (3,7) should return true but (3,8) false because 3 and 7 are in the same column, but 3 and 8 are not. Also (7,3) should be false because for (x,y), xcorresponds to element in 1st row and y in 2nd row
EDIT2: I see isPresent = any(ismember(a.', [x y], 'rows')); for the arrray a.
But what if I have this: b=[1 5; 2 6; 3 7; 4 8]. Then how can I ensure that (3,7) is true but (7,3) is false?

The easiest way is to use ismember, but it works on rows instead of columns, so we'll need to transpose the matrix first:
x = 3;
y = 7;
a=[1 2 3 4; 5 6 7 8];
isPresent = any(ismember(a.', [x y], 'rows'));
>> isPresent
isPresent = 1

Related

Building matrices using column vector and matrix in matlab

I have a column vector A (6x1) with values [6 3 10 4 2 8]'; and a matrix B (6x5) with values
B = [1 2 3 0 4 ;
3 7 8 5 0 ;
0 9 1 0 1 ;
5 0 3 1 2 ;
4 6 7 6 4 ;
3 1 2 7 3]
I want to make five matrices with size 6x2 using Matlab.
The first column is vector A
The second column is columns from B, like [A, B(first col)], [A, B(second col)]
First matrix is [6 1; 3 3; 10 0; 4 5; 2 4; 8 3];
2nd matrix is [6 2; 3 7; 10 9; 4 0; 2 6; 8 1]
... and so on
Any help I really appreciate it
You could use a loop
C = NaN( size(B,1), 2, size(B,2) );
for ii = 1:size(B,2)
C(:,:,ii) = [A, B(:,ii)];
end
This gives you a 3D array, where each slice in the 3rd dimension is a 6x2 matrix (for this example) as desired. You would access the nth slice with C(:,:,n).
You can do this slightly more concisely with arrayfun, but it's basically a loop in disguise
C = arrayfun( #(ii) [A, B(:,ii)], 1:size(B,2), 'uni', 0 );
C = cat(3, C{:} );
You could omit the cat function if you're happy with results in a cell array, where you access the nth matrix with C{n}.
You could first make a copy of the columns of A, then concatenate A and B, and reshape:
At = repmat(A, 1, size(B,2));
C = reshape([At;B], 6, 2, []);
Or oneliner:
C = reshape([repmat(A, 1, size(B,2));B], 6, 2, []);
Then retrieve your matrices with C(:,:,k)
you can use this
first_matrix=[A,B(:,1)];
second_matrix=[A,B(:,2)];
third_matrix=[A,B(:,3)];
... and so on

Transfering elements of 2D array into cell array

Is there a way to instantly transfer elements of an n x n numerical array into an n x n cell array and vice versa so that each cell in the cell array has element's row, column, and value? E.g.,
Input:
A=[8 7 8 4 5;
7 0 7 4 4;
4 3 3 8 6;
7 0 10 8 7;
2 1 0 2 8;];
B=cell(5,5);
Output:
B{1}=[1 1 8];
B{2}=[2 1 7];
B{3}=[3 1 4];
B{4}=[4 1 7];
B{5}=[5 1 2];
B{6}=[1 2 7];
and so on...
Here's one approach:
dim=length(A); %//square matrix
cols = repmat(1:dim,dim,1);
rows = cols';
B=reshape(num2cell([rows(:) cols(:) A(:)],2),dim,dim);
You could wrap this piece of code in a function if you are going to use it often, to "instantly" transfer the elements of the matrix to the cell array.
Maybe using arrayfun
[row, col] = ndgrid(1:size(A,1));
B=arrayfun(#(x,y,z) [x y z], row(:), col(:), A(:), 'uni', 0);
Interesting point is that if you use
B=arrayfun(#(x,y,z) [x y z], row, col, A, 'uni', 0);
you get cell array B with the same size as A where each element is in its corresponding element in A.

Find column locations of vector elements inside a matrix

Given a vector such as a = [2 5 9] and a matrix such as
8 11 5
b = 2 6 1
4 9 3
What's the best way to find which column of b contains each element of a? In this example I'd want an output like [1 3 2] because 2 is in the first column, 5 is in the third column, and 9 is in the second column. For my purposes it's safe to assume that a number can only appear in one column.
One approach -
[colID,~] = find(squeeze(any(bsxfun(#eq,b,permute(a,[1 3 2])),1)))
Or if you would like to avoid squeeze and any -
[~,colID,~] = ind2sub([size(b) numel(a)],find(bsxfun(#eq,b(:),a)))
Another way would be to use ismember:
A = [2 5 9];
B = [8 11 5; 2 6 1; 4 9 3];
[~, ind] = ismember(A,B);
[~, col] = ind2sub(size(B), ind)
col =
1 3 2
Another approach:
[~, index] = ismember(a, b);
[row, col] = ind2sub(size(b, 1), index);

How do you pick out the ith matrix in a n-dimensional matrix?

For the sake of argument, let's say I have this 3 x 3 x 2 matrix:
A = [5 7 8; 0 1 9; 4 3 6];
A(:,:,2) = [1 0 4; 3 5 6; 9 8 7];
And I'd like to pick out the first matrix (dimension: 3 x 3 x 1), how do I do it?
Short answer, already in the comments, is:
A(:,:,1)
Longer answer:
You can pick out any (hyper)-rectangular subset of a matric by simply specifying the elements you want per dimension, weather there are 1, 2, 3, 4 or more dimensions in the array. In short:
Each dimension is specified, in order. Dimension 1 is specifies row, dimension 2 specifies column, dimensions 3 and up are not usually visualized, so just represnt 3 and up
For each dimension you can specify one of the following: a) A single index number, b) A vector of index numbers, or c) a logical vector the same length as the dimension you are selecting from d) :, which represents all elements in this dimension.
Per dimension specifiers are ANDed together, resulting in a (hyper)-rectangular array.
For example, using your A array:
A = [5 7 8; 0 1 9; 4 3 6];
A(:,:,2) = [1 0 4; 3 5 6; 9 8 7];
To subset the first matrix you need: all of dimension 1 ":", all of dimension 2 ":", and the first element of dimension 3 "1". Therefore:
A(:,:,1) %Returns 5 7 8
% 0 1 9
% 4 3 6
To get the first and third columns of the second page, use:
A(:, [1 3], 2) %Returns 1 4
% 3 6
% 9 7
To get all rows which end in 9 from the first matrax, you can use:
mask = A(:,3,1)==9; %Returns logical [0; 1; 0];
A(mask, :, 1); %Returns [0 1 9;

how to get values of a matrix in MATLAB where the indices are given in a nx2 array

I have a matrix A of size nRows x nCols.
I have a nx2 matrix B which contains indices of the matrix A.
I want to get the values of A at the indices given in B.
lets say,
B = [1, 2;
2, 3;
3, 4]
A(1,2) = 1
A(2,3) = 2
A(3,4) = 1
I want to know any Matlab command which gives the following, given A and B (I don't want to use loops):
[1 2 1]
I guess this is what you are looking for:
A(sub2ind(size(A),B(:,1),B(:,2)))
This is what you want:
A = [1,2; 3, 4; 5, 6; 7,8; 9,0]; % this is your N by 2 matrix
B = [1,1; 1,2; 2,1; 3, 1; 4,2]; % these are your indexes
A(sub2ind(size(A), B(:,1), B(:,2)))
A =
1 2
3 4
5 6
7 8
9 0
B =
1 1
1 2
2 1
3 1
4 2
ans =
1
2
3
5
8