Comparing 2 columns of a matrix

조회 수: 4 (최근 30일)
Gautami Golani
Gautami Golani 2019년 2월 23일
댓글: Star Strider 2019년 2월 24일
I want to compare 2 columns of my matrix as shown below:
mat.png
For example, I have the number 4 in the first column occurring thrice. I need to compare it's corresponding values in column 2 and obtain the minimum value among them. I then need to store that value in another matrix.
Could someone suggest me a way to get this desired result.
Thanks and regards

채택된 답변

Star Strider
Star Strider 2019년 2월 23일
Try this (using the accumarray (link) function):
A = [randi(10, 20, 1) rand(20,1)] % Create Data
[A1u,~,idx] = unique(A(:,1)); % Unique Values & Indices
MinVals = accumarray(idx, A(:,2), [], @(x)min(x)); % Find Minimum Of Each
Result = [A1u, MinVals] % Desired Result
Use your own matrix for ‘A’.
  댓글 수: 4
Gautami Golani
Gautami Golani 2019년 2월 24일
Thanks a lot for the explanation.
Star Strider
Star Strider 2019년 2월 24일
As always, my pleasure.

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

추가 답변 (1개)

Image Analyst
Image Analyst 2019년 2월 23일
You can do this easily with unique() and histogram() or histcounts(). Since you didn't include your data in a way that's easy for us to code up, I assume you'd prefer to do it yourself, so give it a try. If you run into problems, attach your data and explain the problem(s).
  댓글 수: 1
Gautami Golani
Gautami Golani 2019년 2월 23일
My code is as shown below. It's just a part of my entire program.
one.png
I'm trying to compare all the 3 values of the number 4.0000 in column 2 and find out the min of them.
Example: For 4.000 the corresponding values in column 2 are 0.0042, 0.0028 and 0.0015.
So my new matrix should have the min of these 3, which is 0.0015.
Thanks

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by