Put rows together in a matrix
이전 댓글 표시
I have constructed a matrix of various values and have sorted based on the values of the first column. As you can see, there are repeated values in the first column. What I would like to do is to "put the rows together" that have the same value in the first column.
For example, currently my sorted matrix, M, is as follows:
M =
0 NaN NaN NaN NaN -50.0903 -0.0576 NaN 3.0000
0.0637 NaN NaN -0.5110 49.9888 NaN NaN NaN 2.0000
0.0637 NaN NaN NaN NaN -50.0488 -0.3238 NaN 3.0000
0.1274 NaN NaN NaN NaN -50.0456 -1.0427 NaN 3.0000
0.1911 NaN NaN -1.4978 49.9976 NaN NaN NaN 2.0000
0.1911 NaN NaN NaN NaN -49.8547 -1.5354 NaN 3.0000
0.2548 NaN NaN NaN NaN -49.9960 -2.0440 NaN 3.0000
0.3185 NaN NaN -2.4941 49.9332 NaN NaN NaN 2.0000
0.3185 NaN NaN NaN NaN -50.0354 -2.2987 NaN 3.0000
0.3822 NaN NaN NaN NaN -49.8377 -2.9497 NaN 3.0000
What I would like, for example for the value of 0.0637 (in the first column) to have the the second row be replaced by the following:
0.0637 NaN NaN -0.5510 49.9888 -50.0488 -0.3238 NaN 13.0000
where 13.000 = 2^2 + 3^2 (i.e. the values of the last column in rows 2 and 3, squared and summed).
I would like this to be repeated for each row of repeating first column values and then construct the new M matrix (knowing that it will have fewer rows).
Is this possible? Please let me know if you need more information, and thanks for your help!
댓글 수: 4
James Kepper
2016년 10월 26일
James Tursa
2016년 10월 26일
Are there ever more than two rows repeated? If so, is the last value in that row the sum square of it and all the previous repeated matching rows?
James Kepper
2016년 10월 27일
James Kepper
2016년 10월 27일
답변 (1개)
KSSV
2016년 10월 27일
clc; clear all ;
data= [ 0 NaN NaN NaN NaN -50.0903 -0.0576 NaN 3.0000
0.0637 NaN NaN -0.5110 49.9888 NaN NaN NaN 2.0000
0.0637 NaN NaN NaN NaN -50.0488 -0.3238 NaN 3.0000
0.1274 NaN NaN NaN NaN -50.0456 -1.0427 NaN 3.0000
0.1911 NaN NaN -1.4978 49.9976 NaN NaN NaN 2.0000
0.1911 NaN NaN NaN NaN -49.8547 -1.5354 NaN 3.0000
0.2548 NaN NaN NaN NaN -49.9960 -2.0440 NaN 3.0000
0.3185 NaN NaN -2.4941 49.9332 NaN NaN NaN 2.0000
0.3185 NaN NaN NaN NaN -50.0354 -2.2987 NaN 3.0000
0.3822 NaN NaN NaN NaN -49.8377 -2.9497 NaN 3.0000] ;
M = data ;
%%Get indices of repeated rows
idx = find(diff(data(:,1))==0) ;
for i = 1:length(idx)
% replace 6th and 7 th column
M(idx(i),6:7) = data(idx(i)+1,6:7) ;
% replace 9th column
M(idx(i),9) = M(idx(i),9)^2+data(idx(i)+1,9)^2 ;
end
댓글 수: 2
James Kepper
2016년 10월 27일
KSSV
2016년 10월 28일
Replacing the other columns is straight forward, you can follow the way, how the columns 6,7 and 9 th replaced.
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!