필터 지우기
필터 지우기

How to determine repeated value in row of an array

조회 수: 3 (최근 30일)
Chau Chin Haw
Chau Chin Haw 2020년 12월 14일
편집: Ive J 2020년 12월 15일
I have a data of
1 1 2 2
2 2 3 1.5
3 3 4 2.2
4 4 5 6.8
5 4 6 1
6 5 7 5.1
7 6 8 2.5
8 7 9 3.3
9 8 10 2.8
10 8 11 1.4
11 10 12 3.2
12 11 13 2.7
13 12 14 1.9
14 13 15 4.5
How can i create a code to have it to check on each row if the value is repeated.
As an example:
The value that i wanted to check is on row 4 column row 2 and 3.
The value selected is 4 and 5. These value are then being compared with the value of the row below it.
If the value in the following row which is row 5 have value that are same with the value above which is 4, it will continue this comapring process with the value 4 and 6 with another row until there is no other identical appear.
After identifying all repeative value, the value in column 4 of those repeative value are being added together.
  댓글 수: 4
Ive J
Ive J 2020년 12월 14일
Can you provide an example along with algorithm steps? For instance:
% Step 1
A = [1 1 2 2;
3 2 1 4];
% step 2
% what to do
% step N
Chau Chin Haw
Chau Chin Haw 2020년 12월 15일
%step 1
A =
[1 1 2 2
2 2 3 1.5
3 3 4 2.2
4 4 5 6.8
5 4 6 1
6 5 7 5.1
7 6 8 2.5
8 7 9 3.3]
%Step 2
%Check value in row 4 with the value in row 5
%If there are similar value in row 5 (value 4), comparing continue with row 5 and 6
%If none, end
%Step 3
%Sum all the value in column 5 of the repeative value
Sum = 6.8+1+5.1+2.5+3.3;

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

채택된 답변

Ive J
Ive J 2020년 12월 15일
편집: Ive J 2020년 12월 15일
Your arrays is N*4 and not N*5, so you may want to modify your example. Also add/modify conditions if necessary.
A = [1 1 2 2
2 2 30 1.5
3 20 4 2.2
4 4 5 6.8
5 4 6 1
6 20 70 5.1
7 6 30 2.5
8 7 9 3.3]
vals = [4 ,20, 30, 70, 5];
sumVals = nan(numel(vals), 1); % NaN for values not satisfying the condition
for i = 1:numel(vals)
checkMe = find(any(A == vals(i), 2)); % find all rows containing vals(i)
if ~isempty(diff(checkMe)) && all(diff(checkMe) == 1)% if vals(i) appreas in consecutive rows and appears at least twice
sumVals(i, 1) = sum(A(checkMe, end)); % sum over last column
end
end
sumVals
10.0000
NaN
NaN
NaN
7.8000

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by