How to rearrange the elements of a matrix in Matlab according to the order of elements in another matrix? - performance

I have a vector A in Matlab of dimension (mxn)x1 composed by real numbers greater or equal then zero, e.g. m=3,n=4 A=[1;0;0;0;0.4;0.7;0.5;0.6;0.8;0;1;6] which looks like
A=[1
0
0
---
0
0.4
0.7
---
0.5
0.6
0.8
---
0
1
6]
We can see that A is composed by n subvectors of dimension m. I have a vector B of dimension gx1, with g greater or equal than m, composed by ones and zeros such that the total number of ones is equal to m, e.g. g=9 B=[1;0;0;0;0;0;0;1;1] which looks like
B=[1
0
0
0
0
0
0
1
1]
I want to create a matrix C of dimension gxn in which the entries of each subvector of A are placed in correspondence of the ones in g for each column of B, e.g.
C=[1 | 0 | 0.5 | 0
0 | 0 | 0 | 0
0 | 0 | 0 | 0
0 | 0 | 0 | 0
0 | 0 | 0 | 0
0 | 0 | 0 | 0
0 | 0 | 0 | 0
0 | 0.4| 0.6 | 1
0 | 0.7| 0.8 | 6]
Loops are fine only if very fast. Real dimensions of matrices are very large (e.g. mxn=100000, g=50000)

Approach #1 (bsxfun based linear indexing)
C = zeros(g,n) %// Pre-allocate
idx = bsxfun(#plus,find(B),[0:n-1]*g) %// Indices where A elements are to be put
C(idx)= A %// Put A elements
Approach #2 (Direct replacement)
C = zeros(g,n)
C(find(B),:) = reshape(A,m,[])
Pre-allocation: For faster pre-allocation you can do this instead in both of the above mentioned approaches -
C(g,n) = 0;

You can also try the repmat and logical indexing approach. First, reshape your data so that it's in the right matrix form, so in your case 3 x 4, then use B as a logical mask and replicate it for as many times as we have columns in your matrix, then do an assignment. You would have to allocate a matrix that is the size of your desired output before doing this assignment. Something like this:
%// Your example data
m = 3; n = 4;
A = [1;0;0;0;0.4;0.7;0.5;0.6;0.8;0;1;6];
B = logical([1 0 0 0 0 0 0 1 1]).';
%// Relevant code
Am = reshape(A, m, n);
Bm = repmat(B, 1, n);
C = zeros(numel(B), 4);
C(Bm) = Am;
C is the desired result, where we get:
C =
1.0000 0 0.5000 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0.4000 0.6000 1.0000
0 0.7000 0.8000 6.0000
My gut feeling is that this will be slower than the bsxfun approach, but the above is more readable if you're not familiar with how bsxfun works.

Related

How to replace kth diagonal in a matrix in octave?

I am trying to replace the sub and super diagonals of a matrix in Octave.
This is the code I am using:
A=[-3 -2 -1 0 1 2 3;0.1 0.2 0.2 0.5 0.6 -0.1 0]'
P=zeros(4,4)
for (k=1:7)
j=A(k,1)
diag(P,j)=A(k,2)
end
This is the error I got: diag(0,_): subscripts must be either integers 1 to (2^63)-1 or logicals
But all the little parts are okay. diag(P,-3) works fine, but when I ask to replace in the loop it refuses!
What can I do about it? Is this: diag(P,j)=e, not the right code to substitute super and sub diagonals?
The reason you're getting an error is that diag(P,j) is not a reference to the diagonal of P, it is a function that returns the values on that diagonal. So what you're doing is assigning the value A(k,2) to the return value of the function and, since it's never assigned to a variable name, the value is lost and nothing changes.
To fix your loop, you would need to provide indices into P and assign to those. One way is to use logical indexing to tell MATLAB which values in P to change. For example,
P = zeros(4)
M = logical(diag([1,1,1], -1))
P(M) = 3
gives us
P =
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
M =
0 0 0 0
1 0 0 0
0 1 0 0
0 0 1 0
P =
0 0 0 0
3 0 0 0
0 3 0 0
0 0 3 0
The unfortunate part is that we can't specify both which diagonal we want to create and the size of the resulting matrix, so we have to calculate the number of elements on the diagonal before creating it.
A=[-3 -2 -1 0 1 2 3;0.1 0.2 0.2 0.5 0.6 -0.1 0].'
n=4; % Number of rows/columns in P...
% If we want a non-square matrix, we'll have to do more math
P=zeros(n);
for k=1:2*n-1 % Remove hardcoded values to make the code more general.
j=A(k,1);
diag_length = n-abs(j);
M=diag(true(1,diag_length),j); % Create logical array with true on jth diagonal
P(M)=A(k,2);
end
The result is:
P =
0.5000 0.6000 -0.1000 0
0.2000 0.5000 0.6000 -0.1000
0.2000 0.2000 0.5000 0.6000
0.1000 0.2000 0.2000 0.5000
Another approach is to use spdiags. One of the uses of spdiags takes the columns of one matrix and uses them to build the diagonals of the output matrix. You pass the indices of the diagonals to set, and the matrix of values for each of the diagonals, along with the matrix size.
If we only pass one value for each diagonal, spdiags will only set one value, so we'll have to duplicate the input vector n times. (spdiags will happily throw away values, but won't fill them in.)
A=[-3 -2 -1 0 1 2 3;0.1 0.2 0.2 0.5 0.6 -0.1 0].'
n = 4;
diag_idx = A(:,1).'; % indices of diagonals
diag_val = A(:,2).'; % corresponding values
diag_val = repmat(diag_val, n, 1); % duplicate values n times
P = spdiags(diag_val, diag_idx, n, n);
P = full(P);
That last line is because spdiags creates a sparse matrix. full turns it into a regular matrix. The final value of P is what you'd expect:
P =
0.5000 0.6000 -0.1000 0
0.2000 0.5000 0.6000 -0.1000
0.2000 0.2000 0.5000 0.6000
0.1000 0.2000 0.2000 0.5000
Of course, if you're into one-liners, you can combine all of these commands together.
P = full(spdiags(repmat(A(:,2).', n, 1), A(:,1).', n, n));

Creating adjacency matrix from 2D array

I have an array like the following:
a b c d
e f g h
j k l m
n o p q
My idea is to create an adjacency matrix from this for only horizontal and vertical movement, where the costs are the ASCII value in the destination.
The solution would find the following kind of adjacency matrix (simplified 'a'=1):
a b c d e f g h <- start
a 0 1 0 0 1 0 0 0
b 2 0 2 0 0 2 0 0
c 0 3 0 3 0 0 3 0
d 0 0 4 0 0 0 0 4
e 5 0 0 0 0 5 0 0
f 0 6 0 0 6 0 6 0
g 0 0 7 0 0 7 0 7
h 0 0 0 8 0 0 8 0
^ destination
I removed the last two rows of the original matrix for brevity.
I've come as far as to realize that a row has only one specific cost, and has a maximum of 4 adjacencies. My question is how do I find these adjacencies? If possible, I want to only iterate through the original matrix to save myself from using the exponentially larger adjacency matrix.
As with all good problems I needed some pen and paper. The solution seems to be the following code, where 'M' is the original matrix and 'adj' is the generated adjacency matrix with dimensions x and y:
makeAdjacencyMatrix(M,adj)
for row = 0 .. y do
for col = 0 .. x do
val = M[row,col]
loc = row * x + col
above = loc - x
below = loc + x
if loc + 1 < x * y then
adj[loc, loc + 1] = val
if loc - 1 >= 0 then
adj[loc, loc - 1] = val
if above >= 0 then
adj[loc, above] = val;
if below < x * y then
adj[loc, below] = val
return adj
I will mark question as answered when possible.

Convert binary values to a decimal matrix

Suppose that I have a matrix a= [1 3; 4 2], I convert this matrix to binary format using this code:
a=magic(2)
y=dec2bin(a,8)
e=str2num(y(:))';
The result is :
y =
00000001
00000100
00000011
00000010
e =
Columns 1 through 17
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Columns 18 through 32
0 0 0 0 1 0 0 0 0 1 1 1 0 1 0
Now when I want to get back my original matrix I inverse the functions :
s=num2str(e(:))';
r=bin2dec(s)
The results I got is:
r =
1082
What can I do to get the orignal matrix? not a number
Thank you in advance
You are doing extra processes which destroyed the original structure:
a=magic(2)
y=dec2bin(a,8)
r=bin2dec(y)
Here r is your answer since y has removed the matrix structure of a. To recreate your matrix, you need to:
originalmatrix = reshape(r,size(a))
originalmatrix =
1 3
4 2
I finally got the right solution for my problem and I want to share it in case anyone need it :
a_back=reshape(bin2dec(num2str(reshape(e, 4, []))), 2, 2)
a =
1 3
4 2

Keep random values from matrix

I have a matrix, which contains N entries each with M rows. Each row contains of 0s and 1s. I want to create a second matrix with the same size, but in each row only one 1 should be left, every other value should be 0. Which value should be 1 should be chosen randomly.
E.g.:
0 1 1 0 1
1 1 0 0 1
0 0 1 1 0
->
0 1 0 0 0
1 0 0 0 0
0 0 0 1 0
Read the documentation of find and randperm
%//preallocate the output matrix
out = zeros(size(a));
%for each row, take a random sample from the indices holding value 1
for i = 1:size(a,1)
temp2 = find(a(i,:));
out(i,temp2(randperm(numel(temp2))(1))) = 1;
end
Watch the code in action here

How can I draw a triangle in an image in MATLAB?

I need to draw a triangle in an image I have loaded. The triangle should look like this:
1 0 0 0 0 0
1 1 0 0 0 0
1 1 1 0 0 0
1 1 1 1 0 0
1 1 1 1 1 0
1 1 1 1 1 1
But the main problem I have is that I do not know how I can create a matrix like that. I want to multiply this matrix with an image, and the image matrix consists of 3 parameters (W, H, RGB).
You can create a matrix like the one in your question by using the TRIL and ONES functions:
>> A = tril(ones(6))
A =
1 0 0 0 0 0
1 1 0 0 0 0
1 1 1 0 0 0
1 1 1 1 0 0
1 1 1 1 1 0
1 1 1 1 1 1
EDIT: Based on your comment below, it sounds like you have a 3-D RGB image matrix B and that you want to multiply each color plane of B by the matrix A. This will have the net result of setting the upper triangular part of the image (corresponding to all the zeroes in A) to black. Assuming B is a 6-by-6-by-3 matrix (i.e. the rows and columns of B match those of A), here is one solution that uses indexing (and the function REPMAT) instead of multiplication:
>> B = randi([0 255],[6 6 3],'uint8'); % A random uint8 matrix as an example
>> B(repmat(~A,[1 1 3])) = 0; % Set upper triangular part to 0
>> B(:,:,1) % Take a peek at the first plane
ans =
8 0 0 0 0 0
143 251 0 0 0 0
225 40 123 0 0 0
171 219 30 74 0 0
48 165 150 157 149 0
94 96 57 67 27 5
The call to REPMAT replicates a negated version of A 3 times so that it has the same dimensions as B. The result is used as a logical index into B, setting the non-zero indices to 0. By using indexing instead of multiplication, you can avoid having to worry about converting A and B to the same data type (which would be required to do the multiplication in this case since A is of type double and B is of type uint8).

Resources