Comparing value in matrix in matlab

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 个评论

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?
I want to find is there any value in column 2 have the same value as col2(i) Ans: yes it is possible that there is no match at all
row_check(i) = any(linedata(:,2)==col2(i));
This would be true (at least one match) or false (no match)
Hi, the any fucntion doesnt give me the right output, what i am trying to do here is finding is there is any value in column 2 is same as the value is col2(i)
%col2(i)
7
8
%column2
7
8
8
%this caused the eorror of different element to occur
linedata(:,2) = [7;8;8]
linedata = 3×2
0 7 0 8 0 8
col2 = [7;8;9]
col2 = 3×1
7 8 9
for i = 1:length(col2)
row_check(i) = any(linedata(:,2)==col2(i));
end
row_check
row_check = 1x3 logical array
1 1 0
Looks fine to me.

请先登录,再进行评论。

回答(0 个)

类别

帮助中心File Exchange 中查找有关 Entering Commands 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by