MATLAB Matrix construction advice - matlab

I need to use MATLAB to solve the following matrix equation:
This matrix is nxn, and I can construct most of the matrix using the following MATLAB code:
e = ones(n,1);
A = h^(-2)*spdiags([e (h^2 - 2)*e e], [-1 0 1], n, n);
What's a good way to change the first and last row? Perhaps it would be nice to just add a nice matrix B with the first row as [ 2/h 1/h^2 0 ... 0 0 0 ] and the last row as [ 0 0 0 ... 0 1/h^2 (2h + 1)/h^2] and just take A + B. How would you do this though?

I think the simplest way is best in this case as you aren't modifying much of the matrix you have created:
A(1,:)=A(1,:)+[2/h 1/h^2 zeros(1,n-2)];
A(n,:)=A(n,:)+[zeros(1,n-2) 1/h^2 2/h];
or even replace individual elements rather than rows.

Related

Why code is not giving proper results Matlab?

I know this type of questions may have been answered before but i am a beginner in matlab so please bear my kiddy questions.
I wan to generate a 11*12 matrix from a set of values. i have five different vectors named X,Y Z,u,v.
my code is:
A=zeros(12,11);
for i=1:6
A=[X(i) Y(i) Z(i) 1 0 0 0 0 (-u(i)*X(i)) (-u(i)*Y(i)) (-u(i)*Z(i)),0 0 0 0 X(i) Y(i) Z(i) 1 (-v(i)*X(i)) (-v(i)*Y(i)) (-v(i)*Z(i))];
end
Here for each iteration i want to fill two rows. So it becomes 12 rows in total. But the problem is that
1. it is giving me 22*1 matrix
2. It is giving wrong values
That means it is appending columns in each iteration that i do not want.
Kindly help me to find a 11*12 matrix. Thanks
You are assigning a completely new matrix to A on every iteration, so this will result in what you get.
What you want is to replace the rows each iteration. You can index the matrix to do this:
A(1,:) = [1 2 3 4];
This, for example, will replace the first row with the given values. So you can use
A(i*2-1,:)=[X(i) Y(i) Z(i) 1 0 0 0 0 (-u(i)*X(i)) (-u(i)*Y(i)) (-u(i)*Z(i))];
A(i*2,:)=[0 0 0 0 X(i) Y(i) Z(i) 1 (-v(i)*X(i)) (-v(i)*Y(i)) (-v(i)*Z(i))];
Unfortunately I don't have Matlab here now to see if you could combine those into one line by indexing A(i*2-1:i*2,:) or not.

Function programming from a ranking to binary choice matrix

I have some difficulties programming a function in MATLAB. I want to construct a function that converts a given ranking into a binary choice matrix. For example, when 3 options are ranked in a ranking R(3,1,2) the binary choice matrix should be
[0 1 0;
0 0 0;
1 1 0]
So when element i proceeds element j, the matrix element a(i,j) is 1 and 0 otherwise. Could anyone help me create this function please?
I'm not sure I understand the question, but this seems to do what you want:
r = [3 1 2]; %// ranking
[~, s] = sort(r);
M = bsxfun(#gt, s, s.');
You want something like.
B=zeros(length(R)); %create an nxn matrix
for j=2:length(R)
B(R(1:j-1), R(j))=1;
end

Matlab:Efficient assignment of values in a sparse matrix

I'm working in Matlab and I have the next problem:
I have a B matrix of nx2 elements, which contains indexes for the assignment of a big sparse matrix A (almost 500,000x80,000). For each row of B, the first column is the column index of A that has to contain a 1, and the second column is the column index of A that has to contain -1.
For example:
B= 1 3
2 5
1 5
4 1
5 2
For this B matrix, The Corresponding A matrix has to be like this:
A= 1 0 -1 0 0
0 1 0 0 -1
1 0 0 0 -1
-1 0 0 1 0
0 -1 0 0 1
So, for the row i of B, the corresponding row i of A must be full of zeros except on A(i,B(i,1))=1 and A(i,B(i,2))=-1
This is very easy with a for loop over all the rows of B, but it's extremely slow. I also tried the next formulation:
A(:,B(:,1))=1
A(:,B(:,2))=-1
But matlab gave me an "Out of Memory Error". If anybody knows a more efficient way to achieve this, please let me know.
Thanks in advance!
You can use the sparse function:
m = size(B,1); %// number of rows of A. Or choose larger if needed
n = max(B(:)); %// number of columns of A. Or choose larger if needed
s = size(B,1);
A = sparse(1:s, B(:,1), 1, m, n) + sparse(1:s, B(:,2), -1, m, n);
I think you should be able to do this using the sub2ind function. This function converts matrix subscripts to linear indices. You should be able to do it like so:
pind = sub2ind(size(A),1:n,B(:,1)); % positive indices
nind = sub2ind(size(A),1:n,B(:,2)); % negative indices
A(pind) = 1;
A(nind) = -1;
EDIT: I (wrongly, I think) assumed the sparse matrix A already existed. If it doesn't exist, then this method wouldn't be the best option.

Entropy of Matrix using Matlab

Given a matrix A with dimension m x n and the entries in the matrix lies [0,1]
For example
A = [0.5 0 0 0.5 0
0 0.5 0 0 0.5
1 0 0 0 0]
I would like to calculate sum(sum(a_ij log(a_ij))), where a_ij is the i th row and j th col entry in the matrix A. Since there exist an 0 entry in the matrix, i always get NAN as a result.
How do i consider only non-zero entries to calculate sum(sum(a_ij log(a_ij))) [entropy of the matrix].
To consider only specific elements of a matrix you can use logical indexing. For example if you only want to select non-zero entries of A you can use A(A~=0). So for your problem the solution can be written:
sum(A(A~=0).*log(A(A~=0)));
EDIT: wow that is some kind of coincidence, I've just seen your comment after posting this. Well, glad you've worked it out yourself.
If it is a very large array:
sum(A.*log(A+eps))
which should be faster than indexing.
Another possibility:
x = A(:);
E = x' * log(x + (x==0))

Matrix to Vector Conversion in Matlab

I have a MxN Matrix and would like to convert into a vector MNx1 with all the elements of the row from the Matrix as the elements of the Vector.
I tried using reshape but I was not successful.
Here is the small code snippet and the expected result.
S=[0 1
1 0
1 1
1 1 ]
Expected Result:
S_prime= [ 0 1 1 0 1 1 1 1]
P.S: Using a loop and concatenation is not an option, I am sure there is a easy straight forward technique, which I am not aware.
Thanks
You could try transposing S and using (:)
S = S'
S_prime = S(:)
or for a row vector:
S_prime = S(:)'
Reshape takes the elements column wise so transpose S before reshaping.
>> reshape(S',1,[])
ans =
0 1 1 0 1 1 1 1
reshape(S',1,prod(size(S)))
or shortcut
reshape(S',1,[])
But the question makes me wonder what your original problem is, and if this way really is part of the correct solution to the original problem.
Octave has a very nice function: vec().
The document at http://www.mathcs.emory.edu/~nagy/courses/fall10/515/KroneckerIntro.pdf states the following.
vector x = vec(X)
can be obtained with the MATLAB statement: x = reshape(X, q*n, 1)