11

Let's say I've got a vector like this one:

A = [101:105]

Which is really:

[ 101, 102, 103, 104, 105 ]

And I'd like to use only vector/matrix functions and operators to produces the matrix:

101 102 103 104 105
102 103 104 105 0
103 104 105 0   0
104 105 0   0   0
105 0   0   0   0

or the following matrix:

101 102 103 104 105
0   101 102 103 104
0   0   101 102 103
0   0   0   101 102
0   0   0   0   101

Any ideas anyone?

(I'm very much a novice in MATLAB, but I've been saddled this stuff...)

Shalom Craimer
  • 20,659
  • 8
  • 70
  • 106
  • Sounds like a homework task to me - if not, why are you not allowed to loop it all out? Note that if this is homework, it is still a valid question on SO, but it should have the [homework] tag. – Tomas Aschan Jun 16 '09 at 10:04
  • I am allowed to loop it out; I've been told that loops incur a significant performance penalty. So I've been wracking my brains trying to figure out how to avoid loops, and finally decided to ask on SO. – Shalom Craimer Jun 16 '09 at 10:43
  • 10
    Something tells me that someone with 1834 reputation is not posting homework assignments to SO. – SCFrench Jun 16 '09 at 12:57
  • 1
    Yeah, I know it sounds like a homework thing; It's just a case where I'm taking over the work from a consultant that my company can no longer afford. I don't know enough about MATLAB to not sound like a newbie. – Shalom Craimer Jun 18 '09 at 08:35

4 Answers4

26

hankel(A) will get you the first matrix

triu(toeplitz(A)) will get you the second one.

--Loren

Nathan Fellman
  • 122,701
  • 101
  • 260
  • 319
Loren
  • 1,725
  • 14
  • 6
  • Of course! I completely forgot that these are common enough to have names. It's been years since I did anything serious with these... – Nathan Fellman Jun 16 '09 at 11:46
3

The best solutions are listed by Loren. It's also possible to create these matrices using SPDIAGS:

vec = 101:105;
A = full(spdiags(repmat(vec,5,1),0:4,5,5));  % The second matrix
B = fliplr(full(spdiags(repmat(fliplr(vec),5,1),0:4,5,5)));  % The first matrix

I recall creating banded matrices like this before I found out about some of the built-in functions Loren mentioned. It's not nearly as simple and clean as using those, but it worked. =)

Community
  • 1
  • 1
gnovice
  • 125,304
  • 15
  • 256
  • 359
2

The way I'd go about it is to create a matrix A:

101 102 103 104 105
101 102 103 104 105
101 102 103 104 105
101 102 103 104 105
101 102 103 104 105

And then find a matrix B such that when you multiply A*B you'll get the result you want. Basically do the linear algebra on paper first and then have Matlab do the calculation.

Nathan Fellman
  • 122,701
  • 101
  • 260
  • 319
  • I don't actually know how to use linear algebra to convert A into what I want. It's kinda like trying to do a linearly-increasing shift on each of the rows... I guess I'll try to figure out something for any NxN matrix... not sure if there is one... – Shalom Craimer Jun 16 '09 at 10:53
2

For generating such triangular matrices with such a regular pattern, use the toeplitz function, e.g.

m=toeplitz([1,0,0,0],[1,2,3,4])

for the other case, use rot90(m)

Baz
  • 36,440
  • 11
  • 68
  • 94