function on if and for loops

조회 수: 1 (최근 30일)
ii
ii 2019년 11월 19일
편집: Darshan Sen 2019년 11월 19일
I am trying to run a function where my input variables are x3 and x4. I am trying to run a code for all positive values of x3 AND x4, the function will computes the ratio x1 divided by x2. I have attempted this through if x+2== x1/x2 but keep seeing error so need some help!! THanks
  댓글 수: 1
KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 11월 19일
편집: KALYAN ACHARJYA 2019년 11월 19일
Can you share your efforts here (code)?

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

답변 (1개)

Darshan Sen
Darshan Sen 2019년 11월 19일
편집: Darshan Sen 2019년 11월 19일
Your question doesn't seem very clear to me. x1 and x2 are not clearly defined.
Assuming, you want to define a function that takes in 2 vectors, x3 and x4 and want to return the ratio between only the positive numbers of each vector in another vector, say x, here's the code:
function x = calculate_ratio_of_positive_numbers_plz (x3, x4)
indices_of_positive_numbers_of_x3_and_x4 = x3 > 0 & x4 > 0;
values_at_those_indices_of_x3 = x3(indices_of_positive_numbers_of_x3_and_x4);
values_at_those_indices_of_x4 = x4(indices_of_positive_numbers_of_x3_and_x4);
x = values_at_those_indices_of_x3 ./ values_at_those_indices_of_x4;
end
Hope this helps. :)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by