Compare some parts from one row in two matrices
Show older comments
Hello, I have two matrices, one is
K=[1 4 2 0 1;1 4 0 0 1;2 4 0 0 1;2 4 3 0 1;5 4 2 0 2];
the other is
L=[1 4 0 0 1; 2 4 0 0 1; 4 2 0 0 2].
I want to do somethings:
- get one row from L in a loop;
- extract the first column and the last column of this row;
- compare the extracted number with the related location in every row of K;
- If the numbers are matched, get the index from K.Can anyone help me how to do that, please?Thanks in advance.
7 Comments
José-Luis
on 30 Aug 2017
What have you tried so far?
Jan
on 30 Aug 2017
@Clémentine OU: The instructions seems to be clear an easy. Please post, what you have tried so far and ask a specific question. The readers are more motivated to assist you, when you show any own effort.
Explain point 3: What is the "related location"? How do you want to store the results?
KL
on 30 Aug 2017
Related location meaning first and last column of K, I'd suppose
Clémentine OU
on 30 Aug 2017
Clémentine OU
on 30 Aug 2017
Edited: Clémentine OU
on 30 Aug 2017
Jan
on 30 Aug 2017
Corresponding to what?
Clémentine OU
on 30 Aug 2017
Accepted Answer
More Answers (1)
Jan
on 30 Aug 2017
K = [1 4 2 0 1;1 4 0 0 1;2 4 0 0 1;2 4 3 0 1;5 4 2 0 2];
L = [1 4 0 0 1; 2 4 0 0 1; 4 2 0 0 2];
[match, locK] = ismember(L(:, [1,end]), K(:, [1,end]), 'rows')
Result = locK(match);
9 Comments
KL
on 30 Aug 2017
Probably OP has to use loops for this "homework" :P
Clémentine OU
on 30 Aug 2017
@Clémentine OU: I cannot follow the explanations. I still do not understand, which are the "related locations" and explaining it by the term "corresponding" does not clarify this also.
It would be useful if you create the wanted result manually for the given example input.
@KL: I bet, that this is no homework, because both inputs K and L contain 60% of completely unused data. Data for homework are usually "dense" to reduce the noise. If this is a homework (please, Clémentine, could you clarify this), I offer you a coffee or beer. :-)
Clémentine OU
on 30 Aug 2017
Image Analyst
on 30 Aug 2017
You forgot to answer Jan's questions:
- Is it homework?
- Can you post the expected output for your sample arrays?
Jan
on 30 Aug 2017
I want to check some parts(1st, 2nd and 5th column) of every row
in L whether they are matched at 1st, 2nd and 5th column in every
row of K
I'm still not sure, what this means. What exactly are the "all indices" you want to get? ismember('rows') checks, if the row vectors or L(:, [1,5]) appear anywhere as row vectors in K(:, [1,5]). Please try to explain again, what you want instead. Try it in words and post the wanted output for the given example. Don't give and there is no need for apologies: This is your problem and you are the only one who suffers from not getting the wanted solution.
Clémentine OU
on 31 Aug 2017
Edited: Clémentine OU
on 31 Aug 2017
Jan
on 31 Aug 2017
"OP" is the "original poster", the person who has asked the question.
"Homework" is a question given to pupils or students to be solved at home. If a user posts a homework question, the forum tend to explain, who it can be solved by your own. Posting a working solution could cause troubles, because you submitting this solution would be cheating and you had missed the chance to learn Matlab.
It is strange, that you do not want to post the wanted output for the shown input, although we have asked repeatedly for this. This is not useful. What's wrong with provided the information, which would allow for solving the problem?
Following your explanations here, this is a job for ismember('rows') and I still think that ismember('rows') solves the problem directly. At least you did not explain, what the differences between the result and your wanted results is.
Clémentine OU
on 1 Sep 2017
Edited: Clémentine OU
on 1 Sep 2017
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!