0

I have a set of values in the following pattern.

A   B   C   D
1   5   6   11
2   6   5   21
3   7   3   42
4   3   7   22
1   2   3   54
2   3   2   43
3   4   3   27
4   3   2   14

I exported the every column into MATLAB workspace as follows.

A = xlsread('F:\R.xlsx','Complete Data','A2:A43'); 
B = xlsread('F:\R.xlsx','Complete Data','B2:B43'); 
C = xlsread('F:\R.xlsx','Complete Data','C2:C43'); 
D = xlsread('F:\R.xlsx','Complete Data','D2:D43'); 

I need help with code where the it has to check the Column A, find the lowest D value and output the corresponding B and C values. I need the output to look like.

1   5   6   11
2   6   5   21
3   4   3   27
4   3   2   14

I read through related questions and understand that I need to make it a matrix and sort it based on the element on the 4th column using

sortrows

and get indices of the sorted elements. But I am stuck here. Please Guide me.

2
  • What happened to 22 in D? Anyway, try fliplr(sortrows([D C B A])) Commented Jan 21, 2018 at 20:31
  • It arranges the D column in the order from minimum number to maximum. Thanks much. I can go ahead with that. Is there anyway to only select one row value for one A number. For example, if we consider 4 from A, it has values, 3 7 22 and 3 2 14. So Can I only get the output of 4 3 2 14 ignoring the other because D is minimum in that? @Adiel Commented Jan 21, 2018 at 20:54

1 Answer 1

3
  1. You can export those columns in one go as:

    ABCD = xlsread('F:\R.xlsx','Complete Data','A2:D43');
    
  2. Now use sortrows to sort the rows according to the first and the fourth column.

    req = sortrows(ABCD, [1 4]);
    
  3. ☆ If all elements of the first column exist twice then:

    req = req(1:2:end,:);  
    

    ☆ If it is not necessary that all elements of the first column will exist twice then:

    [~, ind] = unique(req(:,1));
    req =  req(ind,:);
    
Sign up to request clarification or add additional context in comments.

1 Comment

This is clever. unique finds the first element with each unique value, and since they're sorted on D, the first value is the one with the lowest D value. Nice!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.