writing condition function with vector

조회 수: 1 (최근 30일)
CS
CS 2020년 2월 19일
편집: Rik 2020년 2월 24일
Write a function which inputs a vector v and outputs a vector with the indices i which represent a sign change in v.
Write a function which inputs a vector v and outputs a vector with the indices i which represent a sign change in v.
One suggestion is to write conditions on the vector and some kind of shifted version of itself.
please help me writing this function code
  댓글 수: 2
Rik
Rik 2020년 2월 19일
This is your homework assignment. What have you tried so far yourself?
David Hill
David Hill 2020년 2월 19일
Look at what this does:
v<0;%turns your vector into ones and zeros.
look at diff() and find() commands.

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

답변 (1개)

Nishant Gupta
Nishant Gupta 2020년 2월 24일
편집: Rik 2020년 2월 24일
You can use the followoing logic for writing the function:
x = [1 2 -3 4 -5 -6 7 -9];
product = x(1:end-1).*x(2:end);
res = find(product<0) + 1;
  댓글 수: 1
Rik
Rik 2020년 2월 24일
편집: Rik 2020년 2월 24일
@Nishant: it is generally discouraged to post fully working solutions for homework assignments. You can leave this one, since the OP probably already received the complete code in the email alert.
Your code is also shadowing the prod function.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by