1
votes

Are there any efficient way to do the following?

I have this vector:

0.923
0.757
0.552
0.298
0.079
0.925
0.769
0.565
0.297
0.075
0.927
0.777
0.572
0.294
0.072
0.931
0.778
0.57
0.292
0.07
0.933
0.78
0.566
0.293
0.075

I want to split this vector to smaller vectors each one consist of 5 values, and adding 1 in the top and 0 at the end of each vector like this:

1
0.923
0.757
0.552
0.298
0.079
0

1
0.925
0.769
0.565
0.297
0.075
0

1
0.927
0.777
0.572
0.294
0.072
0

1
0.931
0.778
0.57
0.292
0.07
0

1
0.933
0.78
0.566
0.293
0.075
0

can I use cumsum to find the difference between value 1 and 2 in the same vector ? for example, the first vector 0.923 - 1 = 0.077 and form another vector with the answers ?

1
I guess I can use this to find the differences B = diff(A); , right ? - user11234070
Can you explain more precisely what is your final goal ? You want to store the result in a 1D array ? a 2D array ?... - obchardon
those are pressure values, I'll find dp, then this dp will be used in another eqn. - user11234070
I guess diff(A) works to find the differences, but How can I add 1 and zero in those location, do you think splitting the vector to a smaller one is a good idea, or just keep it and add the 1 and zero in those location ? - user11234070

1 Answers

2
votes
v =[0.923
    0.757
    0.552
    0.298
    0.079
    0.925
    0.769
    0.565
    0.297
    0.075
    0.927
    0.777
    0.572
    0.294
    0.072
    0.931
    0.778
    0.57
    0.292
    0.07
    0.933
    0.78
    0.566
    0.293
    0.075];

A = reshape(v,5,[]);
A = [ones(1,size(A,2)) ; A ; zeros(1,size(A,2))]

A =

   1.00000   1.00000   1.00000   1.00000   1.00000
   0.92300   0.92500   0.92700   0.93100   0.93300
   0.75700   0.76900   0.77700   0.77800   0.78000
   0.55200   0.56500   0.57200   0.57000   0.56600
   0.29800   0.29700   0.29400   0.29200   0.29300
   0.07900   0.07500   0.07200   0.07000   0.07500
   0.00000   0.00000   0.00000   0.00000   0.00000
B = diff(A)

B =

  -0.077000  -0.075000  -0.073000  -0.069000  -0.067000
  -0.166000  -0.156000  -0.150000  -0.153000  -0.153000
  -0.205000  -0.204000  -0.205000  -0.208000  -0.214000
  -0.254000  -0.268000  -0.278000  -0.278000  -0.273000
  -0.219000  -0.222000  -0.222000  -0.222000  -0.218000
  -0.079000  -0.075000  -0.072000  -0.070000  -0.075000