필터 지우기
필터 지우기

Comparing elements in one matrix in a loop

조회 수: 5 (최근 30일)
Ancalagon8
Ancalagon8 2018년 9월 18일
편집: Ancalagon8 2018년 9월 18일
Hi All,
I have a large matrix with one column containing only values. I want to make a loop and compare every time if the average of 2nd and 1st value is < than a predifined value. When the average of i and j value is > than the predifined value, to show as a result i and j. Thanks in advance!

채택된 답변

KALYAN ACHARJYA
KALYAN ACHARJYA 2018년 9월 18일
편집: KALYAN ACHARJYA 2018년 9월 18일
A=randi(30,20,1); %Random Matrix for Example
th=12; %Some Predefined value
[rows colm]=size(A);
for i=1:rows-1
c=(A(i,1)+A(i+1,1))/2;
if c > th
fprintf('The i is: %d and the next row i+1 value is: %d \n',i,i+1);
break;
end
end
  댓글 수: 12
KALYAN ACHARJYA
KALYAN ACHARJYA 2018년 9월 18일
편집: KALYAN ACHARJYA 2018년 9월 18일
The first one only case. I have edited the answer, the added break statement. Please check.
Other c condition also possible, can you clarify more.
Ancalagon8
Ancalagon8 2018년 9월 18일
편집: Ancalagon8 2018년 9월 18일
for example doing inside the loop the calculation to find c gives a 2-digit-result like
(11+10)/2= 10.5, ....... ,
....., (46+38)/2=42,
(468+46)/2=257 <-- so here break as the result has 3 digits

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

추가 답변 (0개)

카테고리

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