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 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