Doug's MATLAB Video Tutorials

February 23rd, 2010

Basics: Making a vector the MATLAB way

I recently received an email asking me a question about how to create an array that is composed of different outputs from functions. The user was new to MATLAB, but had a background in programming other languages. This was evident in his code, which used the scalar approach required of languages like C. In this video, I show how we can use vector indexing in MATLAB to express his code in a much more natural way.

2 Responses to “Basics: Making a vector the MATLAB way”

  1. John A replied on :

    How about just doing this…

    vec=[1 2 sin(3:6) cos(7:10)];
    
  2. dhull replied on :

    John,

    That works well also.

    As you can guess, this was an extreme simplification of a much larger problem that someone had asked me. If the problem were really this simple, I would do just as you say. I was hoping to show the general concept in a simple manner that would scale well for the original problem.

    Thanks,
    Doug

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


MathWorks

Doug Hull is a proud MathWorker who is on a mission to help you with MATLAB.

Doug's picture

These postings are the author's and don't necessarily represent the opinions of The MathWorks.