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

Creating a Matrix with for loop

Asked by Diego Yanez on 16 Jan 2013

I have a fairly large vector named blender (45,000+). I have extracted n elements for which blender is greater than b (irrelevant). Now my difficulty is the following:

I am trying to create a 21 x n matrix with each extracted element of blender plus 10 things before, and the 10 things after, hence the reason why each column is 21.

I have been trying variations of the following:

   element=find(blender >= 120);
   NewMatrix = zeros(21, length(element));
   for i = 1:length(element)
       k = element(i);
       NewMatrix(:,i)= Matrix(blender(k-10:k+10));
   end 

then I want to plot one column of the matrix at the time when I hit Enter. This second part I can figure out later, but I would appreciate some help making the Matrix

Thanks

0 Comments

Diego Yanez

Tags

Products

No products are associated with this question.

1 Answer

Answer by Cedric Wannaz on 16 Jan 2013
Edited by Cedric Wannaz on 16 Jan 2013
Accepted answer

If I understand well and if you want to keep it simple with an explicit for loop, what you want to do is something like that I guess:

 element=find(blender >= 120);
 NewMatrix = zeros(21, length(element));
 for ii = 1:length(element)
    k = element(ii);
    r = max(1, k-10) : min(length(blender), k+10) ; % Range of indices +/- 10
                                                    % limited to valid ids.
    NewMatrix(1:length(r),ii) = blender(r);         % Store blender on the
                                                    % valid range; next 
                                                    % elements left to 0.
 end

Cheers,

Cedric

4 Comments

Cedric Wannaz on 16 Jan 2013

You're welcome! About how it works, I just followed your initial idea indeed. The logic is as follows:

  • You want to extract blocks from blender at each k +/-10, k going iteratively through all indices returned by find().
  • During iteration ii, you want a priori to perform something like: NewMatrix(:,ii) = blender(r) , r being the set/range of indices that cover the block k +/-10. This is almost what you implemented.
  • Now there is a small additional difficulty; if k<=10, k-10 is not a valid index (below 1). In other words, when k is too close to element 1 of blender, this "window" that you want to extract must be truncated. The same applies when k is close to the end of blender.
  • For this reason, we define r that is this window, truncated when it has to.
  • Now when there is truncation, blender(r) is not 21 elements long, so you cannot store it in NewMatrix(:,ii) as the : addresses the full length of NewMatrix along the first dimension, which is 21. So we have to replace : with a vector of indices whose length equals the length of r.
Diego Yanez on 17 Jan 2013

Any chance you can help with the second part of my question. I want to plot one column of NewMatrix at the time, cycling through the next column when I hit Enter. This is as far as I got:

   for j=1:length(element)
       figure(1)
       plot(NewMatrix(:,j),'bo--')
       grid on
       keyboard
   end 

This stops at the first column, but does not keep cycling

Cedric Wannaz on 17 Jan 2013

Yes, replace

 keyboard

with

 input( 'Press a key to continue..' )
Cedric Wannaz

Contact us