# for and if loops question

조회 수: 1 (최근 30일)
William 2013년 9월 4일
I'm having a really hard time wrapping my head around how to do this problem, I'm not asking for a direct answer just a step in the right direction. I understand I need to use a for loop to scan through each value of the vector but I don't know how to make it realize if theres also a number just like it in the other two. Heres the question:
This function takes in three different Vectors. Use for loops to scan through the Vectors and copy any value that appears in all three Vectors into a result. You will need 3 for loops for this function.
Can someone help?
##### 댓글 수: 1이전 댓글 -1개 표시이전 댓글 -1개 숨기기
Azzi Abdelmalek 2013년 9월 4일
What are you looking for in these vectors?

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

### 답변 (3개)

Ingrid Tigges 2013년 9월 4일
Is there a reason you are supposed to use 3 for loops? A hint: Have a look at the function INTERSECT. Using this function the task can be done in 2 lines of code without the use of any loop.
##### 댓글 수: 0이전 댓글 -2개 표시이전 댓글 -2개 숨기기

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

William 2013년 9월 4일
Thats required by the assignment I believe, but I'll look into the intersect function.
##### 댓글 수: 1이전 댓글 -1개 표시이전 댓글 -1개 숨기기
Ingrid Tigges 2013년 9월 4일

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

Image Analyst 2013년 9월 5일
편집: Image Analyst 2013년 9월 5일
You're required by the directions to use loops so you'll need something like 3 pairs of double for loops (to compare all 3 possible pairs of vectors) with something like this inside each pair of for loops
if oneVector(j) == theOtherVector(k)
savedNumbers(count) =
okay, that's enough of a hint. Actually you can do it with one set of 3 nested for loops (like they asked you to), but additional if statements, if you're a little more clever about it.

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

### 카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!

Translated by