conditional array extraction based on control array

조회 수: 8 (최근 30일)
Ivan
Ivan 2013년 1월 24일
I have the following problem:
For example, I have three arrays:
x = [1,2,3,4,5,6,7,8,9,10] and its corresponding y values: y = [21,43,77,54,38,24,99,64,77,33] and also a "control" array: c = [3,5,7,9]
Is it possible to somehow extract from 'y' only those entries that have corresponding entries in both 'x' and 'c' array and put them in for example 'z'? In this case z = [77,38,99,77]
Thanks.

채택된 답변

Cedric
Cedric 2013년 1월 24일
편집: Cedric 2013년 1월 24일
For the second part of the question:
z = y(c) ;
What do you mean by "corresponding entries in x "? How do you define correspondence if x and y don't have the same number of elements?

추가 답변 (2개)

Ivan
Ivan 2013년 1월 24일
I mean that only if entry exists in both 'x' and 'c' for instance '3' does, then to copy in this case 77 form 'y' into 'z'.
  댓글 수: 1
Cedric
Cedric 2013년 1월 24일
Ok, so you can achieve that with intersect, as mentioned by Eric, below.

댓글을 달려면 로그인하십시오.


Eric Sampson
Eric Sampson 2013년 1월 24일
If I understand your question correctly, try this:
z=y(intersect(x,c));

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by