Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How do you make an array of vectors

Subject: How do you make an array of vectors

From: Jeff

Date: 2 Feb, 2010 17:54:08

Message: 1 of 13

I hope this question isn't too simple for you math wiz's on here, but here goes:

I want to implement an example from my linear algebra text. The problem is to determine if a given vector can be a linear combination of five other vectors (in 3 dimensions).

I want to create each of the "other" vectors as an independent vertical vector. Then combine them into a 3x5 array. In other words, I want an array of vectors. How do I implement that correctly?

Here's my start:

u(1)=[1;2;1]
u(2)=[-2;-4;-2]
u(3)=[0;2;3]
u(4)=[2;0;-3]
u(5)=[-3;8;16]

First of all, this gives me an assignment error (in A(I)=B, the number of elements in B and I must be the same). Second, surely there is a better way than this:

u1=[1;2;1]
u2=[-2;-4;-2]
u3=[0;2;3]
u4=[2;0;-3]
u5=[-3;8;16]
A=[u1 u2 u3 u4 u5]

Thanks,
-Jeff

Subject: How do you make an array of vectors

From: Steven Lord

Date: 2 Feb, 2010 18:07:34

Message: 2 of 13


"Jeff " <spREMOVEHITSjeffAT@SIGNoptonline.net> wrote in message
news:hk9os0$ia5$1@fred.mathworks.com...
>I hope this question isn't too simple for you math wiz's on here, but here
>goes:
>
> I want to implement an example from my linear algebra text. The problem is
> to determine if a given vector can be a linear combination of five other
> vectors (in 3 dimensions).
>
> I want to create each of the "other" vectors as an independent vertical
> vector. Then combine them into a 3x5 array. In other words, I want an
> array of vectors. How do I implement that correctly?

While you can have a cell array of vectors:

V = {[1; 2; 1], [-2; -4; -2], [0; 2; 3], [2; 0; -3], [-3; 8; 16]}

and combine them into a matrix,

A = [V{:}]

why not simply store them in the matrix form first:

A = [1 -2 0 2 -3; 2 -4 2 0 8; 1 -2 3 -3 16]

and extract them as necessary?

fourthVector = A(:, 4) % extract all rows, column 4 of A

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: How do you make an array of vectors

From: James Tursa

Date: 2 Feb, 2010 18:09:04

Message: 3 of 13

"Jeff " <spREMOVEHITSjeffAT@SIGNoptonline.net> wrote in message <hk9os0$ia5$1@fred.mathworks.com>...
>
> Second, surely there is a better way than this:
>
> u1=[1;2;1]
> u2=[-2;-4;-2]
> u3=[0;2;3]
> u4=[2;0;-3]
> u5=[-3;8;16]
> A=[u1 u2 u3 u4 u5]

What's wrong with this? You've got your columns clearly listed, and the intended grouping to form A clearly listed. Looks like good readable code to me. You might want to put semi-colons at the end of each line though. If you don't like the variable clutter with u1 ... u5 then clear them after using them.

James Tursa

Subject: How do you make an array of vectors

From: ImageAnalyst

Date: 2 Feb, 2010 18:22:13

Message: 4 of 13

I think as long as you have at least 3 vectors that exist in 3
dimensions, some combination of them should be able to produce the
given vector. So here's what I came up with:

% Using Steve's code:
A = [1 -2 0 2 -3; 2 -4 2 0 8; 1 -2 3 -3 16]'
% My solution
sumRows = sum(A, 1)
if all(sumRows)
msgbox('You can do it');
else
msgbox('You can not do it');
end

I think this should work for some general 3D vector. Maybe it might
fail for some special cases (e.g. vector is 0,0,0, or the component
vectors are all orthogonal to the "test" vector, etc.) but you get the
idea.

Subject: How do you make an array of vectors

From: someone

Date: 2 Feb, 2010 18:25:19

Message: 5 of 13

"James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <hk9po0$dvi$1@fred.mathworks.com>...
> "Jeff " <spREMOVEHITSjeffAT@SIGNoptonline.net> wrote in message <hk9os0$ia5$1@fred.mathworks.com>...
> >
> > Second, surely there is a better way than this:
> >
> > u1=[1;2;1]
> > u2=[-2;-4;-2]
> > u3=[0;2;3]
> > u4=[2;0;-3]
> > u5=[-3;8;16]
> > A=[u1 u2 u3 u4 u5]
>
> What's wrong with this? You've got your columns clearly listed, and the intended grouping to form A clearly listed. Looks like good readable code to me. You might want to put semi-colons at the end of each line though. If you don't like the variable clutter with u1 ... u5 then clear them after using them.
>
> James Tursa

The only thing wrong with it is if you need to do something later on in your code as discussed in Q4.6 of the MATLAB FAQ at:

http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: How do you make an array of vectors

From: James Tursa

Date: 2 Feb, 2010 20:06:19

Message: 6 of 13

"someone" <someone@somewhere.net> wrote in message <hk9qmf$dss$1@fred.mathworks.com>...
>
> The only thing wrong with it is if you need to do something later on in your code as discussed in Q4.6 of the MATLAB FAQ at:
>
> http://matlabwiki.mathworks.com/MATLAB_FAQ

Not really applicable since Q4.6 is talking about creating named variables in a loop. OP is not doing this. I assume you mean if OP decides that there are many more vectors to handle later on or something like that???

James Tursa

Subject: How do you make an array of vectors

From: James Tursa

Date: 2 Feb, 2010 20:13:05

Message: 7 of 13

"James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <hk9po0$dvi$1@fred.mathworks.com>...
> "Jeff " <spREMOVEHITSjeffAT@SIGNoptonline.net> wrote in message <hk9os0$ia5$1@fred.mathworks.com>...
> >
> > Second, surely there is a better way than this:
> >
> > u1=[1;2;1]
> > u2=[-2;-4;-2]
> > u3=[0;2;3]
> > u4=[2;0;-3]
> > u5=[-3;8;16]
> > A=[u1 u2 u3 u4 u5]
>
> What's wrong with this? You've got your columns clearly listed, and the intended grouping to form A clearly listed. Looks like good readable code to me. You might want to put semi-colons at the end of each line though. If you don't like the variable clutter with u1 ... u5 then clear them after using them.
>
> James Tursa

Another way without the u's, but keeping the vector data together for readability:

A = zeros(3,5);
A(:,1) = [1;2;1];
A(:,2) = [-2;-4;-2];
A(:,3) = [0;2;3];
A(:,4) = [2;0;-3];
A(:,5) = [-3;8;16];

James Tursa

Subject: How do you make an array of vectors

From: Jeff

Date: 3 Feb, 2010 17:00:19

Message: 8 of 13

Hey all, I thought I posted a reply here, but I don't see it in the list.

To clarify, I didn't like my solution because, as you all surmised, it wasn't flexible enough. I couldn't think of how to say that when I made my original post.

Thanks for all the great solutions. I read about cell arrays and that suits me best. With that I think I can manipulate each vector individually and then create the full array whenever needed.

I'm loving Matlab's array indexing features. How come no body thought of this sooner?

Subject: How do you make an array of vectors

From: Matt J

Date: 3 Feb, 2010 17:29:05

Message: 9 of 13

"Jeff " <spREMOVEHITSjeffAT@SIGNoptonline.net> wrote in message <hkca33$l1h$1@fred.mathworks.com>...

> I'm loving Matlab's array indexing features. How come no body thought of this sooner?
===============

Sooner than 1980? Computer power was so limited back then, it would have been hard to do much with it...

Subject: How do you make an array of vectors

From: Walter Roberson

Date: 3 Feb, 2010 17:38:54

Message: 10 of 13

Matt J wrote:
> "Jeff " <spREMOVEHITSjeffAT@SIGNoptonline.net> wrote in message
> <hkca33$l1h$1@fred.mathworks.com>...
>
>> I'm loving Matlab's array indexing features. How come no body thought
>> of this sooner?
> ===============
>
> Sooner than 1980? Computer power was so limited back then, it would have
> been hard to do much with it...

1980? Mathworks was founded in 1984.

On the other hand, Iverson's APL was available by 1965, including an
implementation for the 8008 in 1973.

Subject: How do you make an array of vectors

From: Matt J

Date: 3 Feb, 2010 17:47:02

Message: 11 of 13

Walter Roberson <roberson@hushmail.com> wrote in message <hkccbe$k0$1@canopus.cc.umanitoba.ca>...
> Matt J wrote:
> > "Jeff " <spREMOVEHITSjeffAT@SIGNoptonline.net> wrote in message
> > <hkca33$l1h$1@fred.mathworks.com>...
> >
> >> I'm loving Matlab's array indexing features. How come no body thought
> >> of this sooner?
> > ===============
> >
> > Sooner than 1980? Computer power was so limited back then, it would have
> > been hard to do much with it...
>
> 1980? Mathworks was founded in 1984.
===============

According to Wikipedia, MATLAB was created in the 70s, even before the founding of TMW. Of course, I don't actually know if it looked then as we know it now.



> On the other hand, Iverson's APL was available by 1965, including an
> implementation for the 8008 in 1973.
=========

With the same matrix manipulation capabilities as MATLAB??

Subject: How do you make an array of vectors

From: Steven Lord

Date: 3 Feb, 2010 18:04:26

Message: 12 of 13


"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message
news:hkccqm$dob$1@fred.mathworks.com...
> Walter Roberson <roberson@hushmail.com> wrote in message
> <hkccbe$k0$1@canopus.cc.umanitoba.ca>...
>> Matt J wrote:
>> > "Jeff " <spREMOVEHITSjeffAT@SIGNoptonline.net> wrote in message
>> > <hkca33$l1h$1@fred.mathworks.com>...
>> >
>> >> I'm loving Matlab's array indexing features. How come no body thought
>> >> of this sooner?
>> > ===============
>> >
>> > Sooner than 1980? Computer power was so limited back then, it would
>> > have been hard to do much with it...
>>
>> 1980? Mathworks was founded in 1984.
> ===============
>
> According to Wikipedia, MATLAB was created in the 70s, even before the
> founding of TMW. Of course, I don't actually know if it looked then as we
> know it now.

Take a look at about 6.5 minutes into the video associated with this Cleve's
Corner article:

http://www.mathworks.com/company/newsletters/news_notes/clevescorner/dec04.html

The next Cleve's Corner article, from January 2006, talks about The
MathWorks and the continued evolution of MATLAB. You can read it and the
rest of the Cleve's Corner articles here:

http://www.mathworks.com/company/newsletters/news_notes/clevescorner/

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: How do you make an array of vectors

From: us

Date: 3 Feb, 2010 18:18:05

Message: 13 of 13

"Matt J "
> > On the other hand, Iverson's APL was available by 1965, including an
> > implementation for the 8008 in 1973.
> =========
> With the same matrix manipulation capabilities as MATLAB??

yes(!) - and - given the very(!) different hw environment, it was way ahead of its time (i still keep a prime number generator, which i programmed in 1970)...

anyhow, ML was mentioned (most likely for the first time) in richard j harris's book 'a primer of multivariate statistics', including
- some examples and comparisons to other stats packages
- this reference
Moler, C. (1982). MATLAB user's guide (Tech. Rept. No. CS81-1, Rev). Department of Computer Science, University of New Mexico, Albuquerque

just a reminder - and - panta rei...
us

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us