Using the diag and commands!

3 views (last 30 days)
Ryan
Ryan on 29 Mar 2013
So apparently, I can not use the diag and fliplr commands in my program. I have to actually have the program execute the same things just not using those commands. So I am thinking a for loop will need to be used then, but I do not know how. It will always be a 4 x 4 matrix that is stored in MS if that helps.
x=sum(MS);
y=sum(MS');
z=diag(MS);
p=diag(fliplr(MS));
So that is what I had, but I need to change the z and p without using diag and fliplr, and I need to get the same matrix!
Any help is appreciated!

Answers (1)

Walter Roberson
Walter Roberson on 29 Mar 2013
Hint: if you have N rows, then the elements on the diagonal are (N+1) elements apart.
  2 Comments
Ryan
Ryan on 29 Mar 2013
ok well I got that part now. This is how I did it:
[ll]=length(MS);
for i=1:1:ll
z1(i,i) = MS(i,i);
z2 = z1(z1~=0);
end
But I can't figure out how to get the opposite diagnols now without using the fliplr command. I've tried switching the i=1:1:ll, to i=ll:-1:1, or something like that. How would I do this part now?
Walter Roberson
Walter Roberson on 29 Mar 2013
What is z2, and why are you overwriting it on each iteration? And what if there is a 0 on the diagonal ?
Your variable name "ll" looks like the number 11 :(
Hint: if you have N rows, then the elements on the opposite diagonal are (N-1) elements apart.

Sign in to comment.

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!