How to perform this kind of matrix division in Matlab? - matlab

This problem is probably less to do with Matlab and more to do with matrix algebra (which I mostly forget from my college courses). Say I have a m x n matrix X and a m x 1 matrix B. How would I divide the X by B such that all the elements of the ith row of X are piecewise divided by the ith row of B, resulting in another m x n matrix Y?
E.g.
X = [2 4 8; 3 9 27; 4 16 64]
B = [2; 3; 4]
X ? B = [2/2 4/2 8/2; 3/3 9/3 27/3; 4/4 16/4 64/4]
ans =
1 2 4
1 3 9
1 4 16

Better not use repmat - it is slow and allocates additional memory for the workspace. You can use bsxfun, which is an inbuilt function, so it is faster and avoids the extra workspace:
X = [2 4 8; 3 9 27; 4 16 64]
B = [2; 3; 4]
bsxfun(#rdivide, X, B)
ans =
1 2 4
1 3 9
1 4 16

Junuxx's comment pointed me in the right direction. The solution I used to get what I wanted is:
B_prime = repmat(B,1,3)
X ./ B_prime
ans =
1 2 4
1 3 9
1 4 16
I'd still like to know what this kind of operation is called (if it even has a formal name).

X is m x n and B is m x 1
size(X,2) gives the value of n i.e. number of columns
So, you need to do:
X./repmat(B,1,size(X,2))

X = [2 4 8; 3 9 27; 4 16 64]
B = [2; 3; 4]
Result= X./B(:,ones(1,3)) %is faster then repmat
Result =
1 2 4
1 3 9
1 4 16

Related

How to align vectors with asynchronous time stamp in matlab?

I would like to align and count vectors with different time stamps to count the corresponding bins.
Let's assume I have 3 matrix from [N,edges] = histcounts in the following structure. The first row represents the edges, so the bins. The second row represents the values. I would like to sum all values with the same bin.
A = [0 1 2 3 4 5;
5 5 6 7 8 5]
B = [1 2 3 4 5 6;
2 5 7 8 5 4]
C = [2 3 4 5 6 7 8;
1 2 6 7 4 3 2]
Now I want to sum all the same bins. My final result should be:
result = [0 1 2 3 4 5 6 7 8;
5 7 12 16 ...]
I could loop over all numbers, but I would like to have it fast.
You can use accumarray:
H = [A B C].'; %//' Concatenate the histograms and make them column vectors
V = [unique(H(:,1)) accumarray(H(:,1)+1, H(:,2))].'; %//' Find unique values and accumulate
V =
0 1 2 3 4 5 6 7 8
5 7 12 16 22 17 8 3 2
Note: The H(:,1)+1 is to force the bin values to be positive, otherwise MATLAB will complain. We still use the actual bins in the output V. To avoid this, as #Daniel says in the comments, use the third output of unique (See: https://stackoverflow.com/a/27783568/2732801):
H = [A B C].'; %//' stupid syntax highlighting :/
[U, ~, IU] = unique(H(:,1));
V = [U accumarray(IU, H(:,2))].';
If you're only doing it with 3 variables as you've shown then there likely aren't going to be any performance hits with looping it.
But if you are really averse to the looping idea, then you can do it using arrayfun.
rng = 0:8;
output = arrayfun(#(x)sum([A(2,A(1,:) == x), B(2,B(1,:) == x), C(2,C(1,:) == x)]), rng);
output = cat(1, rng, output);
output =
0 1 2 3 4 5 6 7 8
5 7 12 16 22 17 8 3 2
This can be beneficial for particularly large A, B, and C variables as there is no copying of data.

Replicate Kronecker tensor with repmat in MATLAB

I am trying to replicate the Kron product using only repmat and reshape and I believe I am pretty close but I can't manage to do the last correct reshape.
Particularly I have problem in reshaping A
To make things simple let suppose we have
A=[1 3; 2 4]
B=[5 10; 10 5]
so my kron(A,B) is going to be a 4x4 matrix.
kron=[5 10 15 30
10 5 30 15
10 20 20 40
20 10 40 20]
I am proceeding this way:
Y=repmat(B,2,2)
X=A(:);
X=repmat(X,1,2)';
X=X(:);
X=repmat(X,1,2);
which gives me the following 8x2 Matrix:
X= [1 1
1 1
2 2
2 2
3 3
3 3
4 4
4 4]
I can't just figure out how to do the correct reshape to obtain my 4x4 matrix:
X=[1 1 3 3
1 1 3 3
2 2 4 4
2 2 4 4]
Then I will be able to compute: X.*Y=kron(A,B)
Here's one approach using the powerful trio of bsxfun, permute and reshape -
M = bsxfun(#times,B,permute(A,[3 4 1 2]));
out = reshape(permute(M,[1 3 2 4]),size(A,1)*size(B,1),[]);
If you are hell bent on using repmat, perform the calculation of M with it, like so -
M = repmat(B,[1 1 size(A)]).*permute(repmat(A,[1 1 size(B)]),[3 4 1 2])
Verify output by comparing against kron for generic matrix sizes -
>> A = rand(4,5);
>> B = rand(6,7);
>> M = bsxfun(#times,B,permute(A,[3 4 1 2]));
>> out = reshape(permute(M,[1 3 2 4]),size(A,1)*size(B,1),[]);
>> out_kron = kron(A,B);
>> max(abs(out(:) - out_kron(:)))
ans =
0
Here's one using matrix-multiplication and as such must be pretty efficient -
[mA,nA] = size(A);
[mB,nB] = size(B);
out = reshape(permute(reshape(B(:)*A(:).',mB,nB,mA,nA),[1 3 2 4]),mA*mB,[])
If you don't want to use any loops or bsxfun/arrayfun-solutions, you can do as follows:
[ma,na] = size(A);
[mb,nb] = size(B);
Y = repmat(B,ma,mb);
X = reshape(repmat(reshape(repmat(A(:),1,mb)',ma*mb,na),nb,1),ma*mb,na*nb);
X.*Y

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.

Got confused with a vector indexed by a matrix, in Matlab

The following codes runs in Matlab:
a = [1 2 3 4]
b = [ 1 2 3; 1 2 3; 1 2 3]
a(b)
The result of a(b) is a matrix:
[ 1 2 3; 1 2 3; 1 2 3]
Can anyone explain what happened here? Why a vector can be indexed by a matrix, how to interpret the result?
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, what you have there is a vector of a = [1 2 3 4]. When you try to use b to access the vector, what you are essentially doing is a lookup. The output is basically the same size as b, and what you are doing is 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.
Notice that the return value of a(b) is the same size as b.
a(b) simply takes each element of b, call it b(i,j), as an index and returns the outputs a(b(i,j)) as a matrix the same size as b. You should play around with other examples to get a more intuitive feel for this:
b = [4 4 4; 4 4 4];
a(b) % Will return [4 4 4; 4 4 4]
c = [5; 5];
a(c) % Will error as 5 is out of a's index range

Relabel a matrix, replacing all unique numbers with 1..N

I'm using MATLAB. I have a matrix with N elements filled with numbers [1; N], but there are only K unique number between them (K is much less than N). What is an efficient way to relabel the matrix so that it contains only numbers [1; K]? Equal numbers should become equal, and not equal should become not equal.
Example for N = 10, K = 4:
[1 4 8 9 4 1 8 9 4 1] -> [1 2 3 4 2 1 3 4 2 1]
N ~ 1 000 000
K ~ 10 000
Use the third output argument of unique:
a=[1 4 8 9 4 1 8 9 4 1];
[~, ~, b] = unique(a)
b =
1 2 3 4 2 1 3 4 2 1