Clear Filters
Clear Filters

Info

This question is closed. Reopen it to edit or answer.

Extracting certain value from diagonal and perform some mathematics operation

1 view (last 30 days)
Hi everyone, I have a matrix with the dimension of 2*NW*NL. I want to get the certain element from the diagonal of the matrix to perform some mathematics operation. The element I want to get from the diagonal is 3+(2*NW)i. This means that if I put NW=5 and NL=4, it should be have a loop counting NL from 0 until 4, the element I want to get from the diagonal is at 3rd, 13th, 23rd and 33rd. These element I get from the diagonal of matrix needed to be done for some mathematical operation. However, the code below get the result where all the diagonal of the matrix has perform the mathematics operation, and the mathematical operation is not as same as I mention later. The mathematics operation is +3, +2, +1, -3, -2 and -1. Anyone can help me to edit my code? Thanks. The following is my code:
NW = 5;
NL = 4;
i = NL-1;
a = 3+(2*NW);
c = a*i;
z = 4;
for d = 0:i
for b = 1:c
if z == 1
H(b,b) = H(b,b) + 3;
elseif z == 2
H(b,b) = H(b,b) + 2;
elseif z == 3
H(b,b) = H(b,b) + 1;
elseif z == 4
H(b,b) = H(b,b) - 3;
elseif z == 5
H(b,b) = H(b,b) - 2;
elseif z == 6
H(b,b) = H(b,b) - 1;
end
end
end
  1 Comment
Darshan Sen
Darshan Sen on 25 Nov 2019
Hello Kien Wei Lai. I'd really like to help you out but I'm not sure I completely understand your question. It would be wonderful if you could kindly fill up an example matrix H (I presume that's the matrix you're referring to) with some values and show us what you want the expected result to be after the desired operation has been performed. I hope that would be more understandable. Thank you. :)

Answers (0)

This question is closed.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!