简体   繁体   English

MATLAB如果函数&,矩阵尺寸必须一致错误

[英]MATLAB if function with &, Matrix dimensions must agree error

My code works when I type 键入时我的代码有效

if size(k)==size(k1) 
    disp('They match');
end

or 要么

if k-k1==0
    disp('They match');
end

but if I type in two conditions at the same time like this, 但是如果我同时输入两个条件,

if size(k)==size(k1) & k-k1==0
    disp('They match');
end

I get an error saying 我说错了

Matrix dimensions must agree. 矩阵尺寸必须一致。

Error in practice (line 32) if size(k)==size(k1) & k-k1==0 如果size(k)== size(k1)&k-k1 == 0,则实践错误(第32行)

FYI, the dimension of k and k1 are both 1x717 double. 仅供参考, kk1的尺寸均为1x717两倍。 I checked it. 我检查了

So I want to make an if statement that includes two conditions at the same time, but I am experiencing an error. 因此,我想制作一个同时包含两个条件的if语句,但是遇到错误。 Two && won't work cuz two && is for scalars, but my k and k1 are vectors. 两个&&将不起作用,因为两个&&是标量,但是我的kk1是向量。

Any help will be appreciated 任何帮助将不胜感激

when you compare two vectors the result would be also a vector (a logical vector), but an if condition accept scalar logical values, so you can use all function. 比较两个向量时,结果也将是一个向量(逻辑向量),但是if条件接受标量逻辑值,因此可以使用all函数。

if all(size(k)-size(k1)==0) && all(k-k1==0)
    disp('They match');
end

You should always use && in a loop, '&' is used only for logical operation AND. 您应该始终在循环中使用&& ,'&'仅用于逻辑运算AND。

I have tested this and it works: 我已经对此进行了测试,并且可以正常工作:

k = rand(1,10);
k1 = k;

if all(size(k)-size(k1)==0) && all(k-k1==0)
    disp('They match');
end

because when you do this: 因为当您这样做时:

>> k-k1==0

ans =

  1×10 logical array

   1   1   1   1   1   1   1   1   1   1

So the if does not know which value to refer to. 因此, if不知道要引用哪个值。 But when you do 但是当你这样做

>> all(k-k1==0)

ans =

  logical

   1

It gives a unique answer for all the elements of the vector. 它为向量的所有元素提供了唯一的答案。


Important Note: 重要的提示:

Comparing numbers is not a good idea for making decisions on the loops, because of the Floating Point Error problem . 由于浮点错误问题 ,比较数字不是决定循环的好主意。

A Better War to Handle it 处理它的更好的战争

If you read about the floating point error problem, you will see that sometimes, 2.000 == 2.000 results to false . 如果您读到有关浮点错误的问题,有时会看到2.000 == 2.000结果为false In order to fix that you can do as follows: 为了解决该问题,您可以执行以下操作:

tolerance = 0.0001;
if all(size(k)-size(k1)==0) && all(abs(k-k1)<=tolerance)
    disp('They match');
end 

You first define an acceptable tolerance value depending on the nature of the problem you are trying to solve and then instead of comparing the subtract to zero, you compare the absolute value of the abstract to the tolerance. 首先根据要解决的问题的性质定义一个可接受的公差值,然后将摘要的绝对值与公差进行比较,而不是将减法比较为零。 Thus, the numbers such as 23.0001 and 23.000 would be considered equal. 因此,诸如23.000123.000类的数字将被视为相等。

The problem is size(k) and size(k1) returns 1*2 vectors (number of rows and columns), so size(k)==size(k1) returns two values. 问题是size(k)size(k1)返回1 * 2向量(行和列数),因此size(k)==size(k1)返回两个值。 On the other hand k-k1==0 returns only logical matrix with same dimension as k & k1. 另一方面, k-k1==0仅返回与k&k1尺寸相同的逻辑矩阵。

For example, if k == k1 , you would expect both to be equal. 例如,如果k == k1 ,则期望两者相等。

size(k)==size(k1) % returns 1 1
k == k1  % returns 1

if [1 1] && 1 % gives erros

Alternatively, use isequal which will not give error even if dimensions not matching. 或者,使用isequal ,即使尺寸不匹配也不会产生错误。

isequal(k,k1) % returns 1 if equal, 0 otherwise.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM