how to subtract and insert a data into a matrix

1 回表示 (過去 30 日間)
Abebe kebede
Abebe kebede 2015 年 4 月 28 日
編集済み: the cyclist 2015 年 4 月 28 日
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
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
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 Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by