Another Matrix Subtraction Problem

7 ビュー (過去 30 日間)
Dan Lynn
Dan Lynn 2015 年 10 月 13 日
コメント済み: Star Strider 2015 年 10 月 13 日
note: I asked a similar question earlier, but now I have a problem where the numbers in the first column do not start with 1 and followed by counting numbers (2, 3, 4..etc.)
I have this matrix:
67 0
67 2
67 3
78 3
78 4
90 1
90 6
In the second column, I need to find the highest and lowest number that are associated with the same number in the first column, and then subtract the two. In row 1, the numbers 67 and 0 exist. In row 3, the numbers 67 and 3 exists. I need to subtract the 3 and the 0 and get 3 as my output.
The output should be a matrix that looks like this:
67 3
78 1
90 5
Also if a number in the first column only occurs once
67 0
67 2
67 3
78 3
78 4
90 1
90 6
92 5
Then the number in the second column associated with it in the output matrix should be a 0.
67 3
78 1
90 5
92 0

採用された回答

Star Strider
Star Strider 2015 年 10 月 13 日
編集済み: Star Strider 2015 年 10 月 13 日
As long as the first column are integers, this will work:
M = [ 67 0
67 2
67 3
78 3
78 4
90 1
90 6
92 5];
Mu = unique(M(:,1));
R = accumarray(Mu, [1:length(Mu)], [], @(x)[max(M(M(:,1)==Mu(x),2))-min(M(M(:,1)==Mu(x),2))]);
Result = [Mu R(Mu)]
Result =
67 3
78 1
90 5
92 0
  2 件のコメント
Dan Lynn
Dan Lynn 2015 年 10 月 13 日
Ah okay thank you!
Star Strider
Star Strider 2015 年 10 月 13 日
My pleasure!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by