building a matrix starting from another matrix - matlab

I want to build a square matrix. Let's suppose We have this matrix called nodes
1 4.3434 3.4565
2 6.2234 5.1234
3 10.4332 2.3243
4 7.36543 1.1434
where the column 2 and 3 represents position x and y of nodes n
and a matrix called heads where its elements are some elements of nodes matrix
2 6.2234 5.1234
3 10.4332 2.3243
I created this function to build the matrix of the distance of every nodes from the heads
function [distances] = net_dist(nodes,heads)
nnodes = length(nodes(:,1));
distances = zeros(nnodes);
for i = 1 : nnodes
for j = 1 : nnodes
if nodes(i,1) == nodes(j,1) && ismember(nodes(j,1),heads(:,1))
distances(i,j) = sqrt((nodes(i,2) - nodes(j,2))^2 + (nodes(i,3) - nodes(j,3))^2);
elseif (nodes(i,1) == nodes(j,1) || nodes(i,1) ~= nodes(j,1)) && ismember(nodes(j,1),heads(:,1))
distances(i,j) = sqrt((nodes(i,2) - nodes(j,2))^2 + (nodes(i,3) - nodes(j,3))^2);
elseif (nodes(i,1) == nodes(j,1) || nodes(i,1) ~= nodes(j,1)) && ~ismember(nodes(j,1),heads(:,1))
distances(i,j) = 1E9;
end
end
end
return;
This function should return the distance of every nodes from a heads. The positions between nodes that aren't heads are filled with number 1E9. I don't understand why when I execute this function instead to receive sqrt values I receive all 0.
Definitely I would obtain such similar thing
1 2 3 4
1 1E9 d d 1E9
2 1E9 0 d 1E9
3 1E9 d 0 1E9
4 1E9 d 0 1E9

You do not get zeros, you get correct distances. You probably think you get zeros, because 1e9 is a large value and when you try to print your matrix you get
distances =
1.0e+09 *
1.0000 0.0000 0.0000 1.0000
1.0000 0 0.0000 1.0000
1.0000 0.0000 0 1.0000
1.0000 0.0000 0.0000 1.0000
You can see that the two 0-entries are true zeros, while the others are approximately 0 down to 4 digits after the coma. Try to print one of the matrix elements and see they are not zeros
distances(1,2)
ans =
2.5126
You can also use nnz function to quickly know how many non-zeros you have
nnz(distances)
ans =
14

Related

Null space calculation for same matrix with different data type inconsistent

I'm running the following code to find the eigenvector corresponding to eigenvalue of 1 (to find the rotation axis of an arbitrary 3x3 rotation matrix).
I was debugging something with the identity rotation, but I'm getting two different answers.
R1 =
1.0000 -0.0000 0.0000
0.0000 1.0000 0.0000
-0.0000 0 1.0000
R2 =
1 0 0
0 1 0
0 0 1
Running the null space computation on each matrix.
null(R1 - 1 * eye(3))
>> 3x0 empty double matrix
null(R2 - 1 * eye(3))
>>
1 0 0
0 1 0
0 0 1
Obviously the correct answer is the 3x0 empty double matrix, but why is R2 producing a 3x3 identity matrix when R1 == R2 ?
It makes sense that the nullspace of a zero matrix (rank 0) is an identity matrix, as any vector x in R^3 will produce A*x = 0.
>> null(zeros(3, 3))
ans =
1 0 0
0 1 0
0 0 1
This would be the case of R2 - eye(3) if R2 is exactly eye(3)
It also makes sense that the nullspace of a full rank matrix is an empty matrix, as no vectors different than 0 will produce A*x = 0:
>> null(eye(3))
ans = [](3x0)
which could be the case of R1 - eye(3) if R1 is not exactly eye(3) so the result is rank 3. For example:
>> R1 = eye(3) + 1e-12*diag(ones(3,1))
R1 =
1.0000 0 0
0 1.0000 0
0 0 1.0000
>> null(R1 - 1 * eye(3))
ans = [](3x0)
>> rank(R1 - 1 * eye(3))
ans = 3

Normalize each slice of a 3D matrix

How do I normalize each slice of a 3D matrix? I tried like this:
a=rand(1,100,3481);
a= (a - min(a)) ./ (max(a)-min(a)); %
By right each slice of matrix should ranges from 0 to 1. But that is not the case, I don't find 1 in some of the slices. As I inspected, min(a) and max(a) returned the respective value in 3D. Thus it should be of no issue using the code above. Is there something I missed for 3D matrix? Thanks in advance!
We need to find the minimum and maximum values for each of those 2D slices and then we can use bsxfun to do those operations in a vectorized manner with help from permute to let the singleton dims align properly to let bsxfun do its broadcasting job (or use reshape there).
Hence, the implementation would be -
mins = min(reshape(a,[],size(a,3)));
maxs = max(reshape(a,[],size(a,3)));
a_offsetted = bsxfun(#minus, a, permute(mins,[1,3,2]));
a_normalized = bsxfun(#rdivide, a_offsetted, permute(maxs-mins,[1,3,2]))
Sample input, output -
>> a
a(:,:,1) =
2 8 2 2
8 3 8 2
a(:,:,2) =
8 1 1 5
4 9 8 6
a(:,:,3) =
7 9 3 5
6 2 6 5
a(:,:,4) =
9 3 4 9
7 1 9 9
>> a_normalized
a_normalized(:,:,1) =
0 1.0000 0 0
1.0000 0.1667 1.0000 0
a_normalized(:,:,2) =
0.8750 0 0 0.5000
0.3750 1.0000 0.8750 0.6250
a_normalized(:,:,3) =
0.7143 1.0000 0.1429 0.4286
0.5714 0 0.5714 0.4286
a_normalized(:,:,4) =
1.0000 0.2500 0.3750 1.0000
0.7500 0 1.0000 1.0000
My option would be without reshaping as it is sometimes bit difficult to understand. I use min max with the dimension you want want to use for normalization with repmat to clone...:
a=rand(1,100,3481);
a_min2 = min(a,[],2);
a_max2 = max(a,[],2);
a_norm2 = (a - repmat(a_min2,[1 size(a,2) 1]) ) ./ repmat( (a_max2-a_min2),[1 size(a,2) 1]);
or if normalization on 3rd dim...
a_min3 = min(a,[],3);
a_max3 = max(a,[],3);
a_norm3 = (a - repmat(a_min3,[1 1 size(a,3)]) ) ./ repmat( (a_max3-a_min3),[1 1 size(a,3)]);

Multiplication of matrices involving inverse operation: getting infinity

In my earlier question asked here : Matlab: How to compute the inverse of a matrix
I wanted to know how to perform inverse operation
A = [1/2, (1j/2), 0;
1/2, (-1j/2), 0;
0,0,1]
T = A.*1
Tinv = inv(T)
The output is Tinv =
1.0000 1.0000 0
0 - 1.0000i 0 + 1.0000i 0
0 0 1.0000
which is the same as in the second picture. The first picture is the matrix A
However for a larger matrix say 5 by 5, if I don't use the identity, I to perform element wise multiplication, I am getting infinity value. Here is an example
A = [1/2, (1j/2), 1/2, (1j/2), 0;
1/2, (-1j/2), 1/2, (-1j/2), 0;
1/2, (1j/2), 1/2, (1j/2), 0;
1/2, (-1j/2), 1/2, (-1j/2), 0;
0, 0 , 0 , 0, 1.00
];
T = A.*1
Tinv = inv(T)
Tinv =
Inf Inf Inf Inf Inf
Inf Inf Inf Inf Inf
Inf Inf Inf Inf Inf
Inf Inf Inf Inf Inf
Inf Inf Inf Inf Inf
So, I tried to multiply T = A.*I where I = eye(5) then took the inverse Eventhough, I don't get infinity value, I am getting element 2 which is not there in the picture for 3 by 3 matrix case. Here is the result
Tinv =
2.0000 0 0 0 0
0 0 + 2.0000i 0 0 0
0 0 2.0000 0 0
0 0 0 0 + 2.0000i 0
0 0 0 0 1.0000
If for 3 by 3 matrix case, I use I = eye(3), then again I get element 2.
Tinv =
2.0000 0 0
0 0 + 2.0000i 0
0 0 1.0000
What is the proper method?
Question : For general case, for any sized matrix m by m, should I multiply using I = eye(m) ? Using I prevents infinity values, but results in new numbers 2. I am really confused. Please help
UPDATE: Here is the full image where Theta is a vector of 3 unknowns which are Theta1, Theta1* and Theta2 are 3 scalar valued parameters. Theta1 is a complex valued number, so we are representing it into two parts, Theta1 and Theta1* and Theta2 is a real valued number. g is a complex valued function. The expression of the derivative of a complex valued function with respect to Theta evaluates to T^H. Since, there are 3 unknowns, the matrix T should be of size 3 by 3.
your problem is slightly different than you think. The symbols (I, 0) in the matrices in the images are not necessarily scalars (only for n = 1), but they are actually square matrices.
I is an identity matrix and 0 is a matrix of zeros. if you treat these matrix like that you will get the expected answers:
n = 2; % size of the sub-matrices
I = eye(n); % identity matrix
Z = zeros(n); % matrix of zeros
% your T matrix
T = [1/2*I, (1j/2)*I, Z;
1/2*I, (-1j/2)*I, Z;
Z,Z,I];
% inverse of T
Tinv1 = inv(T);
% expected result
Tinv2 = [I,I,Z;
-1j*I,1j*I,Z;
Z,Z,I];
% max difference between computed and expected
maxDist = max(abs(Tinv1(:) - Tinv2(:)))
First you should know, whether you should do
T = A.*eye(...)
or
I = A.*1 %// which actually does nothing
These are completely different things. Be sure what you need, then think about the code.
The reason why you get all inf is because the determinant det of your matrix is zero.
det(T) == 0
So from the mathematical point of view your result is correct, as building the inverse requires every element of T to be divided by det(T). Your matrix cannot be inversed. If it should be possible, the error is in your input matrix, or again in your understanding of the actual underlying problem to solve.
Edit
After your question update, it feels like you're actually looking for ctranpose instead of inv.

What is different between the implementation of deconvolution with fft and the deconv function in MATLAB?

I've got stuck in this code:
function [ y ] = mydeconv( c,x )
lx=length(x);
lc=length(c);
%lt=lx+lc;
c=[c zeros(1,lx)];
x=[x zeros(1,lc)];
y = ifft(real((fft(c)) ./(fft(x))));
end
and the result is:
mydeconv([1 2 3 3 2 1],[1 1 1])
ans =
Column 1
NaN + 0.000000000000000i
Column 2
NaN + NaNi
Column 3
NaN + NaNi
Column 4
NaN + 0.000000000000000i
Column 5
NaN + NaNi
Column 6
NaN + NaNi
Column 7
NaN + 0.000000000000000i
Column 8
NaN + NaNi
Column 9
NaN + NaNi
and the result of deconv function simply is:
deconv([1 2 3 3 2 1],[1 1 1])
ans =
1 1 1 1
In principle it should work, I can't understand what is wrong with it.
There are two problems in your code:
Firstly, you should take real part of the IFFT output, not of individual FFTs.
Secondly, you should protect against zero-divide-by-zero cases, which are resulting in NaN in your example.
You can implement both of the above, by modifying the line computing y as follows:
y = real(ifft((eps+fft(c)) ./ (eps+fft(x))));
Note that eps is a small positive number to protect against zero-divide-by-zero cases. With this, the output is:
disp(y)
% 1.0000 1.0000 1.0000 1.0000 0.0000 -0.0000 0.0000 0.0000 0.0000
Since the padded vector x has a length that is a multiple of the original, you end up with zeros in the frequency domain of fft(x). You can avoid this by choosing a different (longer) length when such zeros are observed:
function [ y ] = mydeconv( c,x )
lx=length(x);
lc=length(c);
if (lc >= lx)
lt = lc;
while (1)
xpadded = [x zeros(1,lt-length(x))];
Xf = fft(xpadded);
if (min(abs(Xf)) > 0)
break;
end
lt = lt + 1;
end
cpadded = [c zeros(1,lt-length(c))];
Cf = fft(cpadded);
y = real(ifft(Cf ./ Xf));
y = y(1:lc-lx+1);
else
y = [];
end
end

Checking which rows switched given an original and an altered matrix in Matlab

I've been trying to wrap my head around this for awhile and was hoping to get some insight.
Suppose you have matrix A, then you switched rows until you ended up with matrix B;
A = [1 3 1;
3 2 1;
2 3 1;];
B = [3 2 1;
1 3 1;
2 3 1;];
invA =
0.0000 -1.0000 1.0000
-1.0000 -1.0000 2.0000
3.0000 5.0000 -7.0000
invB =
-1.0000 0.0000 1.0000
-1.0000 -1.0000 2.0000
5.0000 3.0000 -7.0000
How would I document these row switches?. I'm ultimately trying to alter the inverse of B to match with the inverse of A. My conclusion was that given 2 rows switched (aka between rows 1 and 2), the end result of the inverse would be identical except for switching the columns of (1 and 2) of the inverse B.
This is quite a basic algebra question.
You can write your matrix B as a product of a permutation matrix P and A:
B = PA;
(in your example: P = [0 1 0;1 0 0;0 0 1];).
Now you can invert B:
inv( B ) = inv( PA )
The inverse of a product is
= inv(A) * inv(P)
Since matrix P is a permutation matrix: inv(P) = P.'. Thus
= inv(A) * P.'
That is, inv(B) = inv(A) * P.' which means that you apply the permutation P to the columns of inv(A).
Note that a permutation P can represent more than a single switch between rows, moreover, permutations can be multiplies to account for repeated switching of rows.
An important comment: I use inv in this answer to denote the inverse of a matrix. However, when running Matlab and numerically inverting matrices it is un-recommended to use inv function explicitly.