Hi,
I have below data:
1.3
1.5
1.6
1.7
1.7
2.2
2.8
2.8
I want to find the row index of duplicate values (row4 & row5, row row7& row8) and replace these row by mean 1. row4&row5 replace by the average of them, similarly row7&row8
the final output is:
1.3
1.5
1.6
1.7
2.2
2.8

댓글 수: 2

The mean of identical values is the same value. So isn't what you're after simply:
unique(yourvector, 'stable')
Cedric
Cedric 2017년 9월 18일
Do you have any question regarding my answer to your other question here? :

댓글을 달려면 로그인하십시오.

 채택된 답변

Guillaume
Guillaume 2017년 9월 18일

0 개 추천

Assuming that there is more to your question, and that your input is actually a matrix where one of the column has got the indexing values you mention:
[~, ~, group] = unique(yourmatrix(:, idcolumn), 'stable');
newmatrix = splitapply(@(rows) mean(rows, 1), yourmatrix, group)

댓글 수: 9

Mekala balaji
Mekala balaji 2017년 9월 20일
Sir, I get the following error
??? Undefined function or variable 'idcolumn'.
Vick
Vick 2017년 9월 20일
idcolumn represents the index of the column on which you are having the input data. If your are having the input data(1.3 1.5 1.6 1.7 2.2 2.8) on the first column give 1, if in 3rd column give 3. code will work fine...
Mekala balaji
Mekala balaji 2017년 9월 20일
Can it work on 7.10.0.499 (R2010a)?
Guillaume
Guillaume 2017년 9월 20일
편집: Guillaume 2017년 9월 20일
It's a good idea to indicate in your question that you're using an ancient version. No, it can't work in R2010a since splitapply was introduced in R2015b.
This probably works in R2010b:
[~, ~, group] = unique(yourmatrix(:, idcolumn), 'stable');
newmatrix = cell2mat(accumarray(group, (1:size(yourmatrix, 1))', [], @(rows) {mean(yourmatrix(rows, :), 1)}))
Mekala balaji
Mekala balaji 2017년 9월 20일
still same error: ??? Error using ==> keyboard Undefined function or variable 'idcolumn'.
Guillaume
Guillaume 2017년 9월 20일
As Surey told you, you need to replace idcolumn by the index of the column you want to use as the ID. I already had to guess that your input was a matrix and not a vector as in your question. I'm afraid I can't read your mind and guess which column is the one with the indexing values.
Sir, I replaced idcolumn==1,
[~, ~, group] = unique(matrix(:, 1), 'stable');
newmatrix = cell2mat(accumarray(group, (1:size(matrix, 1))', [], @(rows) {mean(matrix(rows, :), 1)}))
??? Error using ==> unique at 34
Unrecognized option.
K>> matrix
matrix =
1 2 3
2 3 4
1 2 4
Guillaume
Guillaume 2017년 9월 20일
Looks like unique didn't have the 'stable' option in 2010. Just remove it. The only difference is that your output matrix will be sorted according to your ID column. If that column is already sorted, it makes no difference.
Mekala balaji
Mekala balaji 2017년 9월 20일
Thanks, Sir, I tried in 2015 version and it works.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

2017년 9월 18일

댓글:

2017년 9월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by