condition on two arrays

2 views (last 30 days)
MiauMiau
MiauMiau on 12 Dec 2017
Edited: John D'Errico on 12 Dec 2017
Hi,
Given three arrays x,z and y:
z = [5,5,7,7,7,11,15,15,29,29,29]
y = [1,2,3,2,1,1,1,1,2,3,1]
x = [5,7,29]
x contains some elements of z uniquely. For these elements, I wanted to count how often each of their appearance in z is accompanied with a "1" for the same index in y. For instance z contains two "5", at index 1 and at index 2, but only index 1 in y is equal to 1, so the result should be "1".
I coded the following, but this gives me completely wrong results:
for i = 1:length(x)
sumElems = sum(z==x(i)&&y==1)
end
  3 Comments
MiauMiau
MiauMiau on 12 Dec 2017
That was an error but how am I not "clear about the problem I am trying to solve"...?
John D'Errico
John D'Errico on 12 Dec 2017
Edited: John D'Errico on 12 Dec 2017
Are you interested ONLY in the elements that lie in x? If you are, then why are you looping over the length of y?
Note that there are some elements of z that are not members of x, yet they too have a corresponding 1 in y.
As I said, the code that you wrote originally will fail to run at all, so we cannot use that to infer what you are looking to get. (I do see that you have now changed the code to loop only over the length of x.)

Sign in to comment.

Accepted Answer

John D'Errico
John D'Errico on 12 Dec 2017
Edited: John D'Errico on 12 Dec 2017
Since it now appears that you care only about those elements of z that also lie in y, the solution is simple. The code you wrote will ALMOST work, IF you save the results properly.
For example, this should work:
sumelems = zeros(size(x));
for i = 1:length(x)
sumElems(i) = sum(z==x(i)&y==1);
end
Note that I used a &, NOT the && operator. && is used only in tests like an if statement, or in a while statement.
Could I have written the above more simply, without using a loop? Yes. But why bother writing code that will be far less readable, for a tiny problem? Don't pre-optimize code just because a solution may seem more "elegant".
  2 Comments
MiauMiau
MiauMiau on 12 Dec 2017
Yes I had the exact same but with two &&! So that was the problem..well for elegance: sometimes I try to learn more effectively begining with simple problems. But thank you!
John D'Errico
John D'Errico on 12 Dec 2017
Edited: John D'Errico on 12 Dec 2017
I guess that knowing when to use & and && is not always obvious.
&& was introduced (as well as ||) to allow tests to short-circuit in an if statement. So, if you had a test like
if A & B
then if A is false, there is no reason to even evaluate the test B. So && short circuits the test, failing if A is false. You use
if A && B
instead, as a more efficient test. Similarly,
if A | B
is not as efficient as
if A || B
because the || operator allows the if statement to not evaluate the test B when A is true. Again, no reason to evaluate a test when you don't care about the answer.
So use "&&" and || essentially only in if and while statements. (I may have missed some other minor cases that I can't think of at the moment.)

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!