how to subtract and insert a data into a matrix

1 view (last 30 days)
I have 600 by 3 matrix which looks like the following:
23.456 10.598 1.890
21.116 11.222 -5.369
41.963 -10.256 11.235
54.256 14.589 15.888
18.953 20.359 14.523
50.142 6.256 9.124
. . .
. . .
. . .
first i want to change the values of each even rows (2,4,6,8...598,600)by the difference between the the values of odd and even rows,i.e row1 - row 2,row3 -row4.......row599-row600. And finally i want to put additional row vector [10.000 10.000 10.000] between each odd and even rows,i.e row1&row2,row3&row4.....row599 & row 600. so finaly i will have 900 by 3 matrix.
  1 Comment
the cyclist
the cyclist on 28 Apr 2015
Just to check exactly what you mean, what should the final values of Row 2 be?
2.3400 -0.6240 7.2590
?

Sign in to comment.

Answers (2)

the cyclist
the cyclist on 28 Apr 2015
Edited: the cyclist on 28 Apr 2015
See my comment above. If that assumption is correct, then here is one way to get what you want:
A = [23.456 10.598 1.890
21.116 11.222 -5.369
41.963 -10.256 11.235
54.256 14.589 15.888
18.953 20.359 14.523
50.142 6.256 9.124];
B = 10*ones(3*size(A,1)/2,size(A,2));
B(1:3:end,:) = A(1:2:end,:);
B(2:3:end,:) = A(1:2:end,:) - A(2:2:end,:)
This is generalizable to any size for A, as long as it has an even number of rows.

Jason
Jason on 28 Apr 2015
Edited: Jason on 28 Apr 2015
Perhaps something like the following?
% Subtracting and replacing even rows
Data = *original data*;
UpdatedData = zeros(size(Data*1.5,3);
UpdatedData(1:3:end,:) = Data(1:2:end,:);
UpdatedData(3:3:end,:) = Data(1:2:end,:) - Data(2:2:end,:);
% Inserting Rows
UpdatedData(2:3:end,:) = repmat([10 10 10], [size(UpdatedData,1)/3,1]);

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!