Finding indices of multiple items in a matrix? - matlab

I have an array of elements and I want to see the indices of them inside another matrix
For example, for
A = [1 2 3]
B = [1 2 3 4 5 3 4 5 1 2 3]
then result array
C = [1 2 3 6 9 10 11]
that give the indices of 1 2 3
Is there any function or a short way to handle?

I think you want:
find(ismember(B, A))
ans =
1 2 3 6 9 10 11

Related

How do I extract the odd and even rows of my matrix into two separate matrices in scilab?

I'm very new to scilab syntax and can't seem to find a way to extract the even and odd elements of a matrix into two separate matrix, suppose there's a matrix a:
a=[1,2,3,4,5,6,7,8,9]
How do I make two other matrix b and c which will be like
b=[2 4 6 8] and c=[1 3 5 7 9]
You can separate the matrix by calling row and column indices:
a=[1,2,3,4,5,6,7,8,9];
b=a(2:2:end);
c=a(1:2:end);
[2:2:end] means [2,4,6,...length(a)] and [1:2:end]=[1,3,5,...length(a)]. So you can use this tip for every matrix for example if you have a matrix a=[5,4,3,2,1] and you want to obtain the first three elements:
a=[5,4,3,2,1];
b=a(1:1:3)
b=
1 2 3
% OR YOU CAN USE
b=a(1:3)
If you need elements 3 to 5:
a=[5,4,3,2,1];
b=a(3:5)
b=
3 2 1
if you want to elements 5 to 1, i.e. in reverse:
a=[5,4,3,2,1];
b=a(5:-1:1);
b=
1 2 3 4 5
a=[1,2,3,4,5,6,7,8,9];
b = a(mod(a,2)==0);
c = a(mod(a,2)==1);
b =
2 4 6 8
c =
1 3 5 7 9
Use mod to check whether the number is divisible by 2 or not (i.e. is it even) and use that as a logical index into a.
The title is about selecting rows of a matrix, while the body of the question is about elements of a vector ...
With Scilab, for rows just do
a = [1,2,3 ; 4,5,6 ; 7,8,9];
odd = a(1:2:$, :);
even = a(2:2:$, :);
Example:
--> a = [
5 4 6
3 6 5
3 5 4
7 0 7
8 7 2 ];
--> a(1:2:$, :)
ans =
5 4 6
3 5 4
8 7 2
--> a(2:2:$, :)
ans =
3 6 5
7 0 7

How to correct a vector index?

Ok so I am clustering data into clusters which are then indexed using a column. The data is in the form of motion vectors and so my data will look like this after being clustered:
[index x y x' y']
for example:
[1 3 5 4 6;
1 4 6 5 7;
2 3 5 4 6;
2 8 9 9 3;
3 2 3 2 4]
in above array there are 3 clusters, with clusters 1 and 2 each containing 2 vectors.
My problem is that I sometimes have to delete clusters based on certain criteria, and may be left with:
[2 3 5 4 6;
2 8 9 9 3;
3 2 3 2 4]
I want to be able to correct the index after deletion, so that it starts at 1 and ends with the number of clusters. So in this case replace the 2s with 1s and 3s with 2s.
Im sure there must be a simple way using a for loop but Ive been trying for a while and can't get ti right?
Assuming your matrix is called data, try this:
>> data = [2 3 5 4 6;
2 8 9 9 3;
3 2 3 2 4]
data =
2 3 5 4 6
2 8 9 9 3
3 2 3 2 4
>> data(:,1) = cumsum(diff(data([1 1:end], 1)) ~= 0) + 1
data =
1 3 5 4 6
1 8 9 9 3
2 2 3 2 4
A simple call to unique will help you do that. You can use the third output of it to assign each unique and new ID using the first column of the new data matrix (index vector) to replace its first column. Also, make sure you use the 'stable' flag so that it assigns IDs in order of occurrence from top to bottom:
%// Data setup
A = [1 3 5 4 6;
1 4 6 5 7;
2 3 5 4 6;
2 8 9 9 3;
3 2 3 2 4];
%-----
B = A(3:end,:); %// Remove first two rows
%// Go through the other IDs and reassign to unique IDs from 1 up to whatever
%// is left
[~,~,id] = unique(B(:,1), 'stable');
%// Replace the first column of the new matrix with the new IDs
B(:,1) = id; %// Replace first column with new IDs
We get:
>> B
B =
1 3 5 4 6
1 8 9 9 3
2 2 3 2 4

break a matrix to sub-matrices with equal 2nd column without using for loop

I have a matrix, L, with two columns. I want to find its sub-matrices have equal values on their 2nd column. I want to do that using MATLAB without any for loop.
example:
L=[1 2;3 2;4 6;5 3;7 3;1 3;2 7;9 7]
then the sub-matrices are:
[1 2;3 2] , [4 6] , [5 3;7 3;1 3] and [2 7;9 7]
You can use a combination of arrayfun + unique to get that -
[~,~,labels] = unique(L(:,2),'stable')
idx = arrayfun(#(x) L(labels==x,:),1:max(labels),'Uniform',0)
Display output -
>> celldisp(idx)
idx{1} =
1 2
3 2
idx{2} =
4 6
idx{3} =
5 3
7 3
1 3
idx{4} =
2 7
9 7
You can use accumarray directly or with a sorted array, depending on you want the order of the rows to be stable, or the order of the submatricxes to be stable.
Say you want the rows to be stable:
>> [L2s,inds] = sort(L(:,2));
>> M = accumarray(L2s,inds,[],#(v){L(v,:)});
>> M(cellfun(#isempty,M)) = []; % remove empty cells
>> celldisp(M)
M{1} =
1 2
3 2
M{2} =
5 3
7 3
1 3
M{3} =
4 6
M{4} =
2 7
9 7

what is the meaning of a(b) in matlab ? where a and b are matrix [duplicate]

This question already has answers here:
Got confused with a vector indexed by a matrix, in Matlab
(2 answers)
Closed 8 years ago.
Suppose:
a =
1 2 3
4 5 6
2 3 4
and
b =
1 3 2
6 4 8
In MATLABa(b) gives:
>> a(b)
ans =
1 2 4
3 2 6
What is the reason for this output?
when you have a matrix a:
a =
1 2 3
4 5 6
7 8 9
and b:
b =
1 3 4
3 2 6
then a(b) is a way of adressing items in a and gives you:
>> a(b)
ans =
1 7 2
7 4 8
to understand this you have to think of a als a single column vector
>> a(:)
ans =
1
4
7
2
5
8
3
6
9
now the first row of b (1 3 4) addresses elements in this vector so the first, the 3rd and the forth element of that single column vector which are 1 7 and 2 are adressed. Next the secound row of b is used as adresses for a secound line in the output so the 3rd, the 2nd and the 6th elements are taken from a, those are 7 4 and 8.
It's just a kind of matrix indexing.
Matrix indexes numeration in 'a' matrix is:
1 4 7
2 5 8
3 6 9
This is a possible duplicate to this post where I gave an answer: Got confused with a vector indexed by a matrix, in Matlab
However, I would like to duplicate my answer here as I think it is informative.
That's a very standard MATLAB operation that you're doing. When you have a vector or a matrix, you can provide another vector or matrix in order to access specific values. Accessing values in MATLAB is not just limited to single indices (i.e. A(1), A(2) and so on).
For example, let's say we had a vector a = [1 2 3 4]. Let's also say we had b as a matrix such that it was b = [1 2 3; 1 2 3; 1 2 3]. By doing a(b) to access the vector, what you are essentially doing is a lookup. The output is basically the same size as b, and you are creating a matrix where there are 3 rows, and each element accesses the first, second and third element. Not only can you do this for a vector, but you can do this for a matrix as well.
Bear in mind that when you're doing this for a matrix, you access the elements in column major format. For example, supposing we had this matrix:
A = [1 2
3 4
5 6
7 8]
A(1) would be 1, A(2) would be 3, A(3) would be 5 and so on. You would start with the first column, and increasing indices will traverse down the first column. Once you hit the 5th index, it skips over to the next column. So A(5) would be 2, A(6) would be 4 and so on.
Here are some examples to further your understanding. Let's define a matrix A such that:
A = [5 1 3
7 8 0
4 6 2]
Here is some MATLAB code to strengthen your understanding for this kind of indexing:
A = [5 1 3; 7 8 0; 4 6 2]; % 3 x 3 matrix
B = [1 2 3 4];
C = A(B); % C should give [5 7 4 1]
D = [5 6 7; 1 2 3; 4 5 6];
E = A(D); % E should give [8 6 3; 5 7 4; 1 8 6]
F = [9 8; 7 6; 1 2];
G = A(F); % G should give [2 0; 3 6; 5 7]
As such, the output when you access elements this way is whatever the size of the vector or matrix that you specify as the argument.
In order to be complete, let's do this for a vector:
V = [-1 9 7 3 0 5]; % A 6 x 1 vector
B = [1 2 3 4];
C = V(B); % C should give [-1 9 7 3]
D = [1 3 5 2];
E = V(D); % E should give [-1 7 0 9]
F = [1 2; 4 5; 6 3];
G = V(F); % G should give [-1 9; 3 0; 5 7]
NB: You have to make sure that you are not providing indexes that would make the accessing out of bounds. For example if you tried to specify the index of 5 in your example, it would give you an error. Also, if you tried anything bigger than 9 in my example, it would also give you an error. There are 9 elements in that 3 x 3 matrix, so specifying a column major index of anything bigger than 9 will give you an out of bounds error.

How do I convert a 2X2 matrix to 4X4 matrix in MATLAB?

I need some help in converting a 2X2 matrix to a 4X4 matrix in the following manner:
A = [2 6;
8 4]
should become:
B = [2 2 6 6;
2 2 6 6;
8 8 4 4;
8 8 4 4]
How would I do this?
In newer versions of MATLAB (R2015a and later) the easiest way to do this is using the repelem function:
B = repelem(A, 2, 2);
For older versions, a short alternative to the other (largely) indexing-based solutions is to use the functions kron and ones:
>> A = [2 6; 8 4];
>> B = kron(A, ones(2))
B =
2 2 6 6
2 2 6 6
8 8 4 4
8 8 4 4
Can be done even easier than Jason's solution:
B = A([1 1 2 2], :); % replicate the rows
B = B(:, [1 1 2 2]); % replicate the columns
Here's one more solution:
A = [2 6; 8 4];
B = A( ceil( 0.5:0.5:end ), ceil( 0.5:0.5:end ) );
which uses indexing to do everything and doesn't rely on the size or shape of A.
This works:
A = [2 6; 8 4];
[X,Y] = meshgrid(1:2);
[XI,YI] = meshgrid(0.5:0.5:2);
B = interp2(X,Y,A,XI,YI,'nearest');
This is just two-dimensional nearest-neighbor interpolation of A(x,y) from x,y ∈ {1,2} to x,y ∈ {0.5, 1, 1.5, 2}.
Edit: Springboarding off of Jason S and Martijn's solutions, I think this is probably the shortest and clearest solution:
A = [2 6; 8 4];
B = A([1 1 2 2], [1 1 2 2]);
A = [2 6; 8 4];
% arbitrary 2x2 input matrix
B = repmat(A,2,2);
% replicates rows & columns but not in the way you want
B = B([1 3 2 4], :);
% swaps rows 2 and 3
B = B(:, [1 3 2 4]);
% swaps columns 2 and 3, and you're done!
Here's a method based on simple indexing that works for an arbitrary matrix. We want each element to be expanded to an MxN submatrix:
A(repmat(1:end,[M 1]),repmat(1:end,[N 1]))
Example:
>> A=reshape(1:6,[2,3])
A =
1 3 5
2 4 6
>> A(repmat(1:end,[3 1]),repmat(1:end,[4 1]))
ans =
1 1 1 1 3 3 3 3 5 5 5 5
1 1 1 1 3 3 3 3 5 5 5 5
1 1 1 1 3 3 3 3 5 5 5 5
2 2 2 2 4 4 4 4 6 6 6 6
2 2 2 2 4 4 4 4 6 6 6 6
2 2 2 2 4 4 4 4 6 6 6 6
To see how the method works, let's take a closer look at the indexing. We start with a simple row vector of consecutive numbers
>> m=3; 1:m
ans =
1 2 3
Next, we extend it to a matrix, by repeating it M times in the first dimension
>> M=4; I=repmat(1:m,[M 1])
I =
1 2 3
1 2 3
1 2 3
1 2 3
If we use a matrix to index an array, then the matrix elements are used consecutively in the standard Matlab order:
>> I(:)
ans =
1
1
1
1
2
2
2
2
3
3
3
3
Finally, when indexing an array, the 'end' keyword evaluates to the size of the array in the corresponding dimension. As a result, in the example the following are equivalent:
>> A(repmat(1:end,[3 1]),repmat(1:end,[4 1]))
>> A(repmat(1:2,[3 1]),repmat(1:3,[4 1]))
>> A(repmat([1 2],[3 1]),repmat([1 2 3],[4 1]))
>> A([1 2;1 2;1 2],[1 2 3;1 2 3;1 2 3;1 2 3])
>> A([1 1 1 2 2 2],[1 1 1 1 2 2 2 2 3 3 3 3])
There is a Reshape() function that allows you to do this...
For example:
reshape(array, [64, 16])
And you can find a great video tutorial here
Cheers