As a clarification, the above Matrix is the first 10 rows of my matrix that is 1036 x 9, so if possible to make it "automatic" by means of a for loop or some other means, that would great! Thanks for the help!
Put rows together in a matrix
13 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (1)
KSSV
el 27 de Oct. de 2016
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 comentarios
KSSV
el 28 de Oct. de 2016
Replacing the other columns is straight forward, you can follow the way, how the columns 6,7 and 9 th replaced.
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!