Filter löschen
Filter löschen

Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

Extracting certain value from diagonal and perform some mathematics operation

1 Ansicht (letzte 30 Tage)
KIEN WEI LAI
KIEN WEI LAI am 25 Nov. 2019
Geschlossen: MATLAB Answer Bot am 20 Aug. 2021
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 Kommentar
Darshan Sen
Darshan Sen am 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. :)

Antworten (0)

Diese Frage ist geschlossen.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by