Extracting Elements From a Matrix in Matlab - matlab

I'm trying to extract elements from the first column of the following matrix using a for loop
[1.0000 1.0000;
0.4401 4.0000;
0.0000 2.0000;
0.0000 3.0000]
But I'm getting inaccurate values for the zero ones (example: 6.2421e-010 instead of zero)
How to fix this?
Code:
for h=1:K
summation=0;
for i=1:F
x(i,1)
summation=summation+x(i,1);
end
end

You don't need a for loop to extract the first column. You could do this:
a = [1.0000 1.0000; 0.4401 4.0000; 0.0000 2.0000; 0.0000 3.0000];
b = a(:,1);
Giving:
b =
1.0000
0.4401
0
0

From what I have read so far:
A = [1.0000 1.0000; 0.4401 4.0000; 0.0000 2.0000; 0.0000 3.0000];
B = A(:,1);
No for loops needed..
if vpa(x(i,1)) < 0.0000003
summation=summation+x(i,1);
else
summation=summation+vpa(x(i,1));

If your matrix numbers are integers you dont need to write all the zeros after the dot.
To extract a column of a matrix use M(:,j) where M is the matrix and j the column you want to extract.
If you want to sum the elements of that column, just do sum(M(:,j))
M= [1 1; 0.4401 4; 0 2; 0 3]
c1=M(:,1)
summation = sum(c1)

Related

MATLAB : Block diagonalize a complex antisymmetric matrix

I am using the following function to block diagonalize antisymmetric matrices.
function [R, RI , S ] = Matrix_block (A)
[U,D]= schur (A);
E=ordeig(double(D)) ;
[R, S]= ordschur (U,D, abs(E)<1000*eps ) ;
RI=R';
The code works perfectly fine for real antisymmetric matrices but fails for complex antisymmetric matrices as follows :-
a = rand(6); a = a-a'; [r,ri,s] = Matrix_block(a);
b = rand(6)+1i*rand(6); b= b-conj(b)'; [r,ri,s] = Matrix_block(b);
How can I correct my code for it to work also for complex matrices ? I want a block-diagonal matrix (of the following form) as the output for both real and complex matrices.
0 e1 -0.0000 -0.0000 0.0000 -0.0000
-e1 0 0.0000 0.0000 -0.0000 0.0000
0 0 -0.0000 e2 0.0000 -0.0000
0 0 -e2 -0.0000 0.0000 -0.0000
0 0 0 0 -0.0000 e3
0 0 0 0 -e3 -0.0000
You need a different algorithm for the complex case. The Matlab documentation says:
If A is complex, schur returns the complex Schur form in matrix T. The complex Schur form is upper triangular with the eigenvalues of A on the diagonal.
Also, I could notice that you cast your matrix D into double(D). This has no real effects since D is already double. Nevertheless I have seen that the ordeig return different values for the eigenvalues depending if you input D or double(D) even for the real case. It is something to dig more deeply.

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.

Create vectors associated with each entry of an array and save them in a new matrix

Say i have a matrix like A = [1 2; 3 4], and that i need to create 4, vectors each one associated to one entrance of the matrix, such that the first one goes from -1..1, and second from -2..2, and so forth. Wath i try was
for j=1:2
for k=1:2
W=linspace(-A(j,k),A(j,k),4)
end
end
the problem with that line is that it not save the data.
Also i need that to create a new matrix, such that every row be one of the vectors that i mentioned.
I know that on octave i can do
W=linspace(-A,A,4)
but in MATLAB it doesn't work
If you want 4 values evenly distributed between A(k) and A(k), then you can use an anonymous function in combination with linspace this way:
fun = #(x) linspace(-A(x), A(x), 4)
b = fun(1:numel(A))
b =
-1.00000 -0.33333 0.33333 1.00000
-3.00000 -1.00000 1.00000 3.00000
-2.00000 -0.66667 0.66667 2.00000
-4.00000 -1.33333 1.33333 4.00000
Assuming you want [-1 0 1], [-2 -1 0 1 2] etc, then I suggest using arrayfun like this:
A = [1 2;3 4];
b = arrayfun(#(n) -A(n):A(n), 1:numel(A), 'UniformOutput',0)
b =
{
[1,1] =
-1 0 1
[1,2] =
-3 -2 -1 0 1 2 3
[1,3] =
-2 -1 0 1 2
[1,4] =
-4 -3 -2 -1 0 1 2 3 4
Your approach didn't work because you're overwriting W everytime you loop. The following works:
V = zeros(numel(A),4);
for k=1:numel(A)
W(k,:) = linspace(-A(k),A(k),4);
end
The reason why I only use one index for A is because you may use linear indexing in MATLAB. Remember to allocate memory before you assign values to a matrix inside a loop. "Growing" matrices are very slow.
You can do it like that
W = zeros(4,4);
a = reshape(A, 1, 4);
for i=1:4
W(i,:) = linspace(-a(i), a(i), 4);
end
and you obtain
W =
-1.0000 -0.3333 0.3333 1.0000
-3.0000 -1.0000 1.0000 3.0000
-2.0000 -0.6667 0.6667 2.0000
-4.0000 -1.3333 1.3333 4.0000
If you want to generate a fixed number of values (say 4) for each entry of A, you can achieve it in one line:
>> bsxfun(#times, linspace(-1,1,4), A(:))
ans =
-1.0000 -0.3333 0.3333 1.0000
-3.0000 -1.0000 1.0000 3.0000
-2.0000 -0.6667 0.6667 2.0000
-4.0000 -1.3333 1.3333 4.0000

building a matrix starting from another matrix

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

Make a general import of upper triangular matrix in MATLAB

I have been trying to make a general import of Ghaul's answer to my earlier question about importing an upper triangular matrix.
Initial Data:
1.0 3.32 -7.23
1.00 0.60
1.00
A = importdata('A.txt')
A =
1.0000 3.3200 -7.2300
1.0000 0.6000 NaN
1.0000 NaN NaN
So you will have to shift the two last rows, like this:
A(2,:) = circshift(A(2,:),[0 1])
A(3,:) = circshift(A(3,:),[0 2])
A =
1.0000 3.3200 -7.2300
NaN 1.0000 0.6000
NaN NaN 1.0000
and then replace the NaNs with their symmetric counterparts:
A(isnan(A)) = A(isnan(A)')
A =
1.0000 3.3200 -7.2300
3.3200 1.0000 0.6000
-7.2300 0.6000 1.0000
I have this, so we get the complete matrix for any size:
A = importdata('A.txt')
for i = (1:size(A)-1)
A(i+1,:) = circshift(A(i+1,:),[0 i]);
end
A(isnan(A)) = A(isnan(A)');
Is this approach the best? There must be something better. I remember someone told me to try not to use for loops in MATLAB.
UPDATE
So this is the result. Is there any way to make it faster without the loop?
A = importdata('A.txt')
for i = (1:size(A)-1)
A(i+1,:) = circshift(A(i+1,:),[0 i])
end
A(isnan(A)) = 0;
A = A + triu(A, 1)';
Here's another general solution that should work for any size upper triangular matrix. It uses the functions ROT90, SPDIAGS, and TRIU:
>> A = [1 3.32 -7.23; 1 0.6 nan; 1 nan nan]; %# Sample matrix
>> A = spdiags(rot90(A),1-size(A,2):0); %# Shift the rows
>> A = A+triu(A,1).' %'# Mirror around the main diagonal
A =
1.0000 3.3200 -7.2300
3.3200 1.0000 0.6000
-7.2300 0.6000 1.0000
Here's one way without loop. If you have a more recent version of Matlab, you may want to check which solution is really faster, since loops aren't as bad as they used to be.
A = A'; %'# transpose so that linear indices get the right order
out = tril(ones(size(A))); %# create an array of indices
out(out>0) = A(~isnan(A)); %# overwrite the indices with the right number
out = out + triu(out',1); %'# fix the upper half of the array