Comparing elements in one matrix in a loop

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 日

0 投票

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

6 件のコメント

KALYAN ACHARJYA
KALYAN ACHARJYA 2018 年 9 月 18 日
The matrix you said having 1 column, so j value always 1.
KALYAN ACHARJYA
KALYAN ACHARJYA 2018 年 9 月 18 日
編集済み: KALYAN ACHARJYA 2018 年 9 月 18 日
What are i and j, as you are assuming?
Look at here for cell array
KALYAN ACHARJYA
KALYAN ACHARJYA 2018 年 9 月 18 日
編集済み: KALYAN ACHARJYA 2018 年 9 月 18 日
Next position is i+1, why j?
Array having length, matrix has size, confirm?
Check my answer, it works as per your defined question (matrix case).
Anyway I am ready to help if I can
KALYAN ACHARJYA
KALYAN ACHARJYA 2018 年 9 月 18 日
In My Example A is 20x1 Double, check the following-
KALYAN ACHARJYA
KALYAN ACHARJYA 2018 年 9 月 18 日
編集済み: KALYAN ACHARJYA 2018 年 9 月 18 日
Check I have edited the answer. You want to find all positions or the first one only? Like the following?
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.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by