Comparing value in matrix in matlab
Show older comments
clc;
clear;
linedata = [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];
bdata = [1 0
2 100
3 200
4 150
5 275
6 250
7 373
8 220
9 140
10 160
11 360
12 210
13 135
14 80
15 220];
fault = 2; %fault occur at w
row = linedata(fault,2) %bus 2 and bus 3 affected
col = linedata(fault,3)
load = bdata(row,2)+bdata(col,2)
col2 = col;
%1
row_check=find(linedata(:,2)==col)
col2 = linedata(row_check,3)
load = load+sum(bdata(col2,2))
%2
row_check=find(linedata(:,2)==col2)
col2 = linedata(row_check,3)
load = load+sum(bdata(col2,2))
%3
for i = 1:length(col2)
i
col2(i)
row_check(i)=find(linedata(:,2)==col2(i));
row_check(i)
end
col2 = linedata(row_check,3)
load = load+sum(bdata(col2,2))
%4
for i = 1:length(col2)
i
col2(i)
row_check(i)=find(linedata(:,2)==col2(i));
end
col2 = linedata(row_check,3)
load = load+sum(bdata(col2,2))
% there is an error in %4
%Unable to perform assignment because the left and right sides have a different number of elements.
%Error in test3 (line 66)
%row_check(i)=find(linedata(:,2)==col2(i));
%this error occure because row_check(i) is trying to find the value of 7 & 8 in column 2
7
8
8
%there are 2 value 8 in this column
Can someone help me to solve this?
5 Comments
Walter Roberson
on 22 Dec 2020
What do you want to do in that case? Store just one of them? Store the entire list? Average the values for those rows?
Question: is it ever possible that there might be no match at all?
Chau Chin Haw
on 22 Dec 2020
Walter Roberson
on 22 Dec 2020
row_check(i) = any(linedata(:,2)==col2(i));
This would be true (at least one match) or false (no match)
Chau Chin Haw
on 23 Dec 2020
linedata(:,2) = [7;8;8]
col2 = [7;8;9]
for i = 1:length(col2)
row_check(i) = any(linedata(:,2)==col2(i));
end
row_check
Looks fine to me.
Answers (0)
Categories
Find more on Entering Commands 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!