MATLAB help - Vector initialization

edited March 2007 in Science & Tech
I'm very new with MATLAB and I currently am having trouble generating vectors. I want to generate a set of vectors, each having ten values. So the idea is, in the first vector, the first 9 values are zero, and the 10th value is 1. I want the subsequent vectors to have 9 values of zero and 1 value of 1, with the value of 1 rotating in position throughout the vector. I also want to be able to do the same with a vector that has 8 zeros and 2 ones, then 7 zeros and three 1's, and so on until I have a vector of 10 ones.
Any help on this would be much appreciated. Thanks.

Comments

  • qparadoxqparadox Vancouver, BC
    edited March 2007
    Hi Jw3Crystal,

    There's lot of ways to do what you want. You may find it easier, rather than creating different vectors, to create matrices (which are really just vectors of vectors).

    A simple way to complete the problem you have above is to use the command CIRCSHIFT. You can use help circshift to get the exact info on what this does. But in short, it just rotates positions by an arbitrary number of positions. So you could do:

    [PHP]
    baseVec=[1; 0; 0; 0; 0; 0; 0; 0; 0; 0]
    vec1=circshift(baseVec,1)
    [/PHP]
    The output should be:
    [0; 1; 0; 0; 0; 0; 0; 0; 0; 0]

    A more elegant method, using matrices could be something like:
    [PHP]
    lonelyOneMat=zeros(10,10) %creates a 10x10 matrix in memory
    lonelyOneMat(1,1)=1 %sets the top left element to 1
    for i=2:size(lonelyOneMat,2) %do this loop for every column
    lonelyOneMat(:,i)=circshift(lonelyOneMat(:,1),i-1); %shift each column vector by i-1 locations
    end
    [/PHP]

    To adapt this for other original baseVectors you can just assign more values to 1 inititialy:
    lonelyOneMat(:,1)=[0;1;0;1;0;0;0;0;0;0]
    !!!! NOTE this won't give you all posibilities for the locations of the two ones, to do that, you really need to create 45 different vectors (10 chose 2). !!!!

    Some things you should learn and experiment with:

    The colon operator (:) does many things in different contexts. In the context above, it selects all the elements in the given dimension of a matrix. You can read up on in in the matlab help

    You never ever want to do "grow" vectors in a matlab for loop. By growing I mean keep making the vector longer. This operation takes a looong time and will really slow down your loops. Assign the memory beforehand (just like you would in C or C++) and you can see orders of magnitude speedups in processing.
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited March 2007
    eye(N) returns an NxN identity matrix. If you take each column or row, it will give you the rotating 1.

    for the other, I'd use the ones() and zeros() command:

    so:

    vec = [ones(1,n) zeros(1,N-n)];

    will give you a vector of length N, that starts with n ones and the rest is filled with zeros.

    Also, you generally want to avoid for loops in matlab, as they're much slower than their matrix operation counterparts.
Sign In or Register to comment.