How to create corresponding equation in a column based on the value of previous column? (Similar to drag in Excel)

조회 수: 3 (최근 30일)
Hi all,
Imagine we have a 1000 x 3 matrix named A. I want to have a specific equation such as A(1,1) * by EquationA if the value in A(1,1) is less than 50 and A(1,1) * by EquationB if the value in A(1,1) is greater than or equal to 50. Then, drag this logic for the entire 1000 rows similar to Excel. How should I do this?

채택된 답변

Stephen23
Stephen23 2021년 5월 25일
The MATLAB approach would be to use logical indexing:
For example,assuming that your "equations" are actually vectorized functions:
Z = eqB(A(:,1));
X = A(:,1)<50; % logical vector
Z(X) = eqA(A(X,1))
  댓글 수: 2
Wolfgang McCormack
Wolfgang McCormack 2021년 5월 28일
@Stephen Cobeldick thank you. So just a quick question, does the first line multiply the entire A's members by eqB? or just the ones above 50?
Now, in the last line, Z all those mathcing X condition will be multiplied by eqA. but is the final matrix a compiled matrix of all Z mutiplied by eqB and eqA? or should I compile/join it manually?
Stephen23
Stephen23 2021년 5월 29일
편집: Stephen23 2021년 5월 29일
"So just a quick question, does the first line multiply the entire A's members by eqB? or just the ones above 50?"
All of them.
"but is the final matrix a compiled matrix of all Z mutiplied by eqB and eqA? or should I compile/join it manually?"
You do not need to join anything manually, the indexing already "joins" the data:
Z(X) = eqA...
% ^ this indexing combines the eqA data into the eqB data.

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by