condition on two arrays

2 ビュー (過去 30 日間)
MiauMiau
MiauMiau 2017 年 12 月 12 日
編集済み: John D'Errico 2017 年 12 月 12 日
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 件のコメント
MiauMiau
MiauMiau 2017 年 12 月 12 日
That was an error but how am I not "clear about the problem I am trying to solve"...?
John D'Errico
John D'Errico 2017 年 12 月 12 日
編集済み: John D'Errico 2017 年 12 月 12 日
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.)

サインインしてコメントする。

採用された回答

John D'Errico
John D'Errico 2017 年 12 月 12 日
編集済み: John D'Errico 2017 年 12 月 12 日
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 件のコメント
MiauMiau
MiauMiau 2017 年 12 月 12 日
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 2017 年 12 月 12 日
編集済み: John D'Errico 2017 年 12 月 12 日
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.)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by