I have this matrix
mpsim =
1.0e+04 *
-2.2331
-0.4261
1.3810
3.1880
4.9951
6.8022
8.6092
this matrix
fvsim =
NaN NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN
0 0 0.9000 0.1000 0 0 0
0 0 0 0.7500 0 0.2500 0
0 0 0 0 0 1.0000 0
0 0 0 0 0.5000 0 0.5000
0 0 0 0 0 0 1.0000
and this matrix
lingsim =
3
3
3
3
3
3
3
3
3
3
4
4
4
4
6
5
6
7
7
I'm trying to use this code but got an error
sizeA=size(mpsim,1);
sizeB=size(fvsim,1);
sizeC=size(lingsim,1);
outputsim = zeros(size(lingsim));
for i=1:sizeC
if lingsim(i)<=sizeB
outputsim(i)=sum(mpsim * fvsim(lingsim(i), :));
else
outputsim(i)=lingsim(i);
end
end
outputsim
In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in ftskutes (line 131)
outputsim(i)=sum(mpsim * fvsim(lingsim(i), :));
How to fix this? Actually I'm assuming that sum(mpsim * fvsim(lingsim(i), :)); is 1x1 but when I try to check it is 1x7.
The problem is that sum() only works in one dimension - so mpsim * fvsim(lingsim(i), :) produces a 7x7 matrix where it then takes the column sums of, resuling in a 1x7 vector.
To get the sum of all elements, you can use
if lingsim(i)<=sizeB
outputsim(i)=sum(sum(mpsim * fvsim(lingsim(i), :)));
else
edit:
i assumed you did take the outer product on purpose. If however you wanted to multiply each element with each other, you have to replace * with .* and transpose one of the two vectors:
outputsim(i)=sum(mpsim' .* fvsim(lingsim(i), :));
When you multiply to vectors, you should be sure to perform the operation you want:
Outer product: (n x 1) * (1 x n) == (n x n)
Inner (dot) product: (1 x n) * (n x 1) == (1 x 1)
Elementwise product: (n x 1) .* (n x 1) == (n x 1)
mpsim is column vector, i.e. n x 1, and fvsim(lingsim(i), :) is a row vector, i.e. 1 x n. Therefore you are calculating the outer product.
If this not what you want, you can take the transpose (.') or the builtin function dot to calculate the dot product independent of the orientation of your vectors.
Related
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
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.
This question already has an answer here:
Matlab element-wise division by zero
(1 answer)
Closed 7 years ago.
I'm using matlab and I have two matrices :
G =
1 1 1 1
1 1 1 1
and the scond:
m =
4 4 4 4
0 0 0 0
I want this result :
x =
1/4 1/4 1/4 1/4
0 0 0 0
What I did so far is this :
x = G ./ m
But it returns :
x =
1/4 1/4 1/4 1/4
NaN NaN NaN NaN
How can I avoid the divison by zero by placing a default value "0" if there is a division by zero ?
You can convert the NaNs back to zero:
x = G ./ m;
x(isnan(x))=0; % thanks to comment by #nkjt
Or, if you have also NaNs in matrix m that you want to save, you can do:
x(m==0)=0;
One option would be to preallocate x and then only use division on the parts where m is not zero.
x = zeros(size(m)); % output is same size as m
n = m~=0; % find indexes
x(n)=G(n)./m(n);
MATLAB: What does the "greater than" symbol in the expression: (1-X).*(X>1) mean or do?
X is a column vector and theoretically each element of X lies in the range 0 to 1 inclusive. Numerically due to finite precision, an element might be slightly in excess of 1.0, so this may be a sort of condition or constraint.
Any ideas?
In the context you describe, it is essentially "selecting" elements greater than 1 and subtracting them from 1; all other elements in the result (those in positions where X <= 1) will be 0 because they have been multiplied by the zero from the result of X > 1. Here is an example:
>> X = [0 0.5 1 1.5 2];
>> X > 1
ans =
0 0 0 1 1
>> (1 - X)
ans =
1.0000 0.5000 0 -0.5000 -1.0000
>> (1 - X) .* (X > 1)
ans =
0 0 0 -0.5000 -1.0000
In the last result, the three zeros in the first result have been multiplied (elementwise) with the first three values in the middle result, and the two ones have been multiplied by the last two values in the middle result. In this way, it has effectively selected the results of (1 - X) in the positions where X > 1 to begin with.
Obviously, if there are never any values of X greater than 1, then the result will always be all zeros.
The expression (X > 1) returns true (logical '1') for every element that is greater than 1, and false (logical '0') otherwise. Therefore, X > 1 produces a boolean vector (of the same size as X) containing '1's where the corresponding elements in X are greater than 1. I strongly recommend that you get more familiar with MATLAB operators.
As for the rest of the expression:
(1 - X) simply returns an vector of the same dimensions as X, where each element is the result of subtracting the corresponding element in X from 1.
.* is an element-wise multiplication, so the final result is a vector (of the same dimensions as X), where the i-th element equals (1 - Xi) if Xi>1, and 0 otherwise.
Example
>> X = [-1 4 0 2 -3];
>> X > 1
ans =
0 1 0 1 0
>> 1 - X
ans =
2 -3 1 -1 4
>> (1 - X) .* (X > 1)
ans =
0 -3 0 -1 0
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