How to create more than one matrix in a row using matlab

时间:2016-04-25 09:27:17

标签: matlab function matrix indexing

I am trying to get a series of vectors which come from the same original, to make an easy example, suppose this vector V= (1,2,3,4,5,6,7,8,9,10) (of course mine is bigger)

The first vector has to look like this:

R1=(1,3,5,7,9)= V(1:1:end) 
The second vector:
R2=(2,4,6,8,10)=V(2:1:end)
The third vector:
R3=(3,5,7,9)=V(3:1:end)
The fourth vector:
R4=(4,6,8,10)=V(4:1:end)
...
R8=(8,10)=V(8:1:end)

So my questions are:

  1. Is there an easier way to get this result?
  2. How can I know the total number of Ri vectors with distance = 1 that can obtained from V?

1 个答案:

答案 0 :(得分:0)

Use Matlab's cell object which can hold a vector in every cell. Use a for loop to fill this cell object gradually.

Code example:

%initialize V
V= [1,2,3,4,5,6,7,8,9,10]; 
%initialize an empty cell of size [10,1]
R= cell(length(V)-2,1); 

%fill the cell
for ii=1:length(R)
   R{ii} = V(ii:2:end); 
end

%prints results
for ii=1:length(R)
   R{ii} 
end

Results (each row is a different vector):

 1     3     5     7     9
 2     4     6     8    10
 3     5     7     9
 4     6     8    10
 5     7     9
 6     8    10
 7     9
 8    10
相关问题