Best function instead of for loop and if loop

조회 수: 2 (최근 30일)
HG-NU
HG-NU 2020년 5월 12일
댓글: HG-NU 2020년 5월 12일
I believe for loop and if condition sometimes not the best option for the computer RAM and Memory.
Is there any function can do my job at least avoiding for loop and if condition or at least one of them?
The job is:
Assume I have this array A.
A=[1 2 6; 1 2 2; 1 2 3]
I want to save the values of the last column of the array A in a new array 'Show', when Only if the values of the last column of array A is between 2 and 5. Other elements less or above the condition will be 0 in array Show.
Is there anyway suggest for the solution please?
  댓글 수: 1
James Tursa
James Tursa 2020년 5월 12일
What have you done so far? What specific problems are you having with your code? A(:,end) is the last column of A. You can save that in Show then manipulate the elements as you like.

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

채택된 답변

Geoff Hayes
Geoff Hayes 2020년 5월 12일
HG-NU - try something like
A=[1 2 6; 1 2 2; 1 2 3];
Show = A(:,end); % <---- get the last column of A
Show(Show < 2 | Show > 5) = 0; % <---- use logical indexing to set elements outside of (2,5) to zero
  댓글 수: 1
HG-NU
HG-NU 2020년 5월 12일
So brilliant.
Thanks alot :)
This is exactly what I want.

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by