How to determine repeated value in row of an array
2 views (last 30 days)
Show older comments
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 Comments
Ive J
on 14 Dec 2020
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
Accepted Answer
Ive J
on 15 Dec 2020
Edited: Ive J
on 15 Dec 2020
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 Comments
More Answers (0)
See Also
Categories
Find more on Multidimensional Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!