The Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Math Forum » Discussions » Software » comp.soft-sys.matlab

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Making a square matrix from two vector
Replies: 22   Last Post: Mar 19, 2013 12:37 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]

Posts: 9,850
Registered: 6/7/07
Re: Making a square matrix from two vector
Posted: Feb 8, 2013 1:40 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

On 2/8/2013 9:42 AM, Jerry wrote:
> "a" is a vector (size 1-by-11) and "b" is also a vector (size 11-by-1),
> in that the first number of these two vectors is the same so "a1"="b1".
> I would like to create a square matrix (size of this matrix is
> 11-by-11), from these two vectors in that "a" is the first row of this
> matrix and "b" is the first column of this matrix. In fact, I am going
> to use this function [F = (a + b) + (a * b)] to fill out the rest of
> this matrix. So, the shape of matrix would be in this way:
> a1=b1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11
> b2 b2a2 b2a3 b2a4 b2a5 ...

b3 b3a2
> b4
> b9
> b10
> b11 ................................. b11a11

>> a=1:4; b=[1:2:8]';
>> [X,Y]=meshgrid(a,b);
>> F = @(x,y) ((x+y) + (x.*y));
>> c=zeros(length(a)); c(:,1)= b; c(1,:)=a;
>> c(2:end,2:end)=arrayfun(F,X(2:end,2:end),Y(2:end,2:end));
>> c

c =
1 2 3 4
3 11 15 19
5 17 23 29
7 23 31 39

I _think_ this is same order as you wanted; reverse X,Y here if the
inner portion of c is the transpose of what you're after...


Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.