how to subtract and insert a data into a matrix

3 次查看(过去 30 天)
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.

回答(2 个)

the cyclist
the cyclist 2015-4-28
编辑:the cyclist 2015-4-28
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 2015-4-28
编辑:Jason 2015-4-28
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]);

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by