how to subtract and insert a data into a matrix
1 次查看(过去 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.
1 个评论
the cyclist
2015-4-28
Just to check exactly what you mean, what should the final values of Row 2 be?
2.3400 -0.6240 7.2590
?
回答(2 个)
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.
0 个评论
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]);
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!