How to pick out the corresponding answers

Dear All,
Thank you for your time.
I would want to find the corresponding answers after comparing a main result.
This is the example:
I am comparing a calculated result A= 2 5 3 9 1 10 and 1 is the smallest value and B= 2 5 7 4 9 5 and C= 5 2 4 7 6 1 are the answers related to A, since smallest A is 1, I would want to pick out the corresponding answers which should be B=9 and C=6. However, I have tried in different ways, I still cannot achieve it. Could somebody help me with this please?
Thank you very much.
Zhe Li

 Accepted Answer

A=[2 5 3 9 1 10]
B=[2 5 7 4 9 5]
C=[5 2 4 7 6 1]
[x,idx]=min(A) % x is the min value and idx is the corresponding index
out1=B(idx)
out2=C(idx)

8 Comments

Hi Azzi,
Thank you very much for answering my question. I can get the answers that I want to using the code you wrote, however, when I implenented it in my code, I got some problems. The answer I am getting are without [ ],e.g. a = 2 5 3 9 1 10. I think that is where the problem is. How would I add on [ ] automatically in my answer? if it is not possible, do you have another way to solve the problem I demonstrated earlier on?
Thank you so much for your help.
Zhe Li
Please tell us what a, b and c are using
whos a b c
If they are of size '1x6' of class 'double' Azzi's code should work.
The are no constructs like "a = 2 5 3 9 1 10" in Matlab. The square brackest do not belong to the value, but they are the concatenation operator. Therefore "[2,3,4]" is simply a vector, which contains 3 elements. It is possible, that your result is displayed as "2 5 3 9 1 10", but this is not a valid syntax in written code. Please check the type and the size of your "A":
class(A)
size(A)
And finally reading the Getting Started chapters of the documentation can be recommended intensively. The basics are explained there very well.
Dear Thorsten,
Thanks for your comment.
I am only using a b c as an example. a b c are the size of 1*6'. But I don't really understand how to 'double' Azzi's code. Thanks
Zhe li
Dear Jan,
Thanks for your comments. I do understand the use of square bracket. The result I am getting is displayed as 2 3 3 9 1 10. and when I am using Azzi's code, I am getting an error message as 'Undefined function or method 'min' for input arguments of type 'cell'' that's why I am thinking the square bracket may have made the difference. do you have any suggestion that why I am getting this error message?
Thank you.
Zhe Li
Zhe, from what are you getting A,B and C ? If those variables are cell array:
A={2 5 3 9 1 10};
B={2 5 7 4 9 5};
C={5 2 4 7 6 1};
[x,idx]=min(cell2mat(A));
out1=B{idx}
out2=C{idx}
Dear Zhe Li,
please tell us the output of
whos a
with a replaced by the name of the real variable. Matlabs variables can belong to different classes, e.g, double, or cell, and depending on what the class is you may need different functions to solve your problem.
Hi Azzi,
Thank you so much for your answer, it works.
Zhe Li

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!