Can somebody explain me this code?

R = arrayfun (@(x) median(A(A(:,1)==x,2)==2), B);
(I've searched for 'arrayfun', but I still don't understand this completely)
Thank you.

1 Kommentar

Rik
Rik am 26 Jun. 2019
What exactly don't you understand? Arrayfun applies the function to every element of your input, so for every element of B it runs that anonymous function.
Because you don't provide any other context it is difficult to explain it more than that.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Bob Thompson
Bob Thompson am 26 Jun. 2019

0 Stimmen

arrayfun is basically a for loop for all elements of an array.
This code is doing the following, starting from the outside:
1) Array fun is working through all elements of B. Each element is being represented as 'x.'
R = arrayfun (@(x) x, B);
2) The median values are being captured A values which are equal to 2.
median(A==2)
3) The range of A values is being limited to those in the second column.
A(:,2)
4) The range of A values is being limited to rows whose first column has a value equal to our element from B.
A(A(:,1)==x,2)
So, overall, this code is finding the median of values of A which are equal to 2, but only in the second column of rows whose first column value is equal to each respective element of B. Overall, all this code is going to do is determine whether 2 is a majority value of the subset of A, because the median command is receiving a logic array as an input, so all values will be either 1 or 0. For elements of R == 1, then 2 is a majority value for the corresponding element of B, and for elements of R == 0, then 2 is not a majority.

5 Kommentare

Guillaume
Guillaume am 26 Jun. 2019
Whoever wrote that line should have commented it to explain what its purpose is. While anybody fluent enough with matlab can understand what each function does, the mental leap from taking the median of a logical array to establish if 2 is the majority value is a bit much to ask of the reader.
Assuming the values in B are unique, that code would have been better written as an accumarray, splitapply or similar type of construct.
Bob Thompson
Bob Thompson am 26 Jun. 2019
Would you say that I'm wrong for guessing that the desired output is meant to determine the majority, or lack there of? I fully recognize that I have no idea why you would want to know this, because I have not context for the line, but calling median on a logical array gives some pretty strong limitations to the outputs. It is an assumption that 'majority' is what is being concluded, but I can think of no other reason why you would want a simple 1 or 0 to the presence of 2 being the median in a set of information. If the median command was looking at the actual values of A I could see more options, but I find myself limited in my creativity here.
Not trying to be a flame with this, just discussing my thoughts.
Guillaume
Guillaume am 26 Jun. 2019
Bearbeitet: Guillaume am 26 Jun. 2019
No, you're totally correct in your interpretetation and I wasn't criticising it in any way.
My point was that whoever initially wrote that code should have commented it and it was extremely poor coding practice not to have done so. As you've demonstrated, the reader has to go through many mental loops to understand the full intent of the line.
The first instinct of anybody reading that code is that it's calculating the median of some numbers. It's only when you look at it carefuly that you realise: hang on! It's taking the median of a logical array! What's going on?
Rik
Rik am 26 Jun. 2019
Just adding another note: median([true false]) returns true, so a strict majority is not required.
I think I would have used mean on the logical array (as that would allow more fine-grained control over the partitions), although I can imagine median might be a bit faster under some circumstances.
It's not clear what B is, I suspect it might be the unique values of column 1 of A, in which case, I would have used:
group = findgroup(A(:, 1));
ismajority2 = splitapply(@(values) nnz(values == 2) >= numel(values)/2, A(:, 2), group);
which is a lot easier to understand. Even if B is not the unique values, I still would first partition A then use the splitapply (or accumarray if you're old fashioned).

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu MATLAB finden Sie in Hilfe-Center und File Exchange

Tags

Gefragt:

am 26 Jun. 2019

Kommentiert:

am 26 Jun. 2019

Community Treasure Hunt

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

Start Hunting!

Translated by