Row combination for repeated values
조회 수: 1 (최근 30일)
이전 댓글 표시
Hello,
I have different variables (date, time, lat, lon) and X for different depths, where each column represents n times (number of different depths) the same variable at a different depth, let say 0 m, 100 m, 500 m. Here a more explicit example :
A = [20200101, NaN, NaN, 1200, NaN, NaN, 90, NaN, NaN, 45, NaN, NaN, 2, NaN, NaN; % 0 m
NaN, 20200101, NaN, NaN, 1200, NaN, NaN, 90, NaN, NaN, 45, NaN, NaN, 3, NaN; % 100 m
NaN, NaN, 20200101, NaN, NaN, 1200, NaN, NaN, 90, NaN, NaN, 45, NaN, NaN, 4 ]; % 500 m
How can I keep the repeated band combinations for date, time, latitude and longitude, so my final output would be, for each unique Date / Time / Lat / Lon :
B = [20200101, 1200, 90, 45, 2, 3, 4]; % Date / Time / Lat / Lon / Val 0m / Val100m / Val500m
I was thinking of using a for loop with find
find(A(i,1)==A(:,2) & A(i,1)==A(:,3) etc.)
But I if I have 50 different depths, it will be a lot of combinations. Does anyone has a simpler way in mind ?
Cheers
댓글 수: 1
Jon
2020년 2월 21일
I'm not understanding what the columns in your A matrix represent. In particular how come the date appears in a different column in each row. Similarly why does the latitude appear in a different column. It looks like they shift over by 1 column in each succesive row, but I don't know if this is always the case, and what it means.
답변 (1개)
Spencer Chen
2020년 2월 21일
Now, you were not very clear on your column structure. I understand it as in:
A = [date.depth1, date.depth2 .. date.depthNtime, lat.depth1, lat.depth2 ..lat.depthN, etc.]
% A = row X (depth and Var)
If that's the case, then something like this code would solve you problem:
nrows = size(A,1);
ndepth = 3;
A2 = reshape(A,nrows,ndepth,[]); % A2 = row X depth X Var
B = squeeze(nansum(A2,2));
The crux of it is to use nansum() to combine and ignore values from other depths. So we restructure the matrix so we can use nansum().
Blessings,
Spencer
댓글 수: 5
Jon
2020년 2월 25일
I'm sorry, but I can not understand the problem description from the above. If you could please provide just one complete example of the input matrix or matrices that you actually would begin with and the final resulting matrix that you are looking for perhaps this would help. I see lots of matrices in the above discussion, but I can't follow what they are trying to illustrate. I think, one, simple, complete example would help.
참고 항목
카테고리
Help Center 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!