简体   繁体   English

为什么这个MATLAB'if'语句不起作用?

[英]Why does this MATLAB 'if' statement not work?

I'm pretty new to MATLAB and I can't figure out why my "if" statement and condition don't work. 我是MATLAB的新手,我无法弄清楚为什么我的“if”语句和条件不起作用。 I have a 100-by-4 matrix ( randQ ). 我有一个100乘4的矩阵( randQ )。 Columns 1 through 3 are random integers, and column 4 contains either a 1 or 2 in each row. 1列到第3列是随机整数,第4列在每行中包含1或2。

I want to change all 1 's into 0 's and all 2 's into 1 's in column 4 in that 100-by-4 matrix. 我想改变所有1的成0的和所有2的成1的中柱4在100乘4矩阵。 What I'm doing is: 我在做的是:

if randQ(:,4) == 1
    randQ(:,4) = 0
elseif randQ(:,4) ==2
    randQ(:,4) = 1
end

It does not throw any errors but the matrix does not change and, in my mind, the conditions make perfect sense. 它不会抛出任何错误,但矩阵不会改变,在我看来,条件非常有意义。 I don't know why it doesn't work. 我不知道为什么它不起作用。 Any explanation would be welcome. 任何解释都会受到欢迎。

Disclaimer 放弃

Some of the other answers contain incorrect/inaccurate claims: 其他一些答案包含不正确/不准确的声明:

As an example, 举个例子,

if [1, 1, 1] == 1
    disp("True")
end

will display "True" . 将显示"True" See below for an explanation. 请参阅下面的解释。


You write 你写

I cant figure out why my "if" statement and condition does not work 我无法弄清楚为什么我的“if”语句和条件不起作用

What's happening is actually quite subtle. 发生的事实上非常微妙。 You need to develop an understanding of several things: 你需要了解几件事情:

  1. what the expression randQ(:,4) == 1 returns, 什么表达式randQ(:,4) == 1返回,
  2. what the "if" statement does when presented with a predicate that is, not a scalar, but an array, 当呈现谓词时,“if”语句的作用是什么,不是标量,而是数组,
  3. what randQ(:,4) = 0 does. 什么randQ(:,4) = 0

Furthermore, you need to start using logical indexing for this kind of operations. 此外,您需要开始使用逻辑索引进行此类操作。

What randQ(:,4) == 1 returns 什么randQ(:,4) == 1返回

Under the assumption that randQ is a 100-by-4 array, the expression randQ(:,4) == 1 returns a 100-by-1 logical array , ie full of (logical) zeros and ones: 假设randQ是一个100乘4的数组,表达式randQ(:,4) == 1返回一个100乘1的逻辑数组 ,即完整的(逻辑)0和1:

  • if the ith entry of this array is a (logical) 1 , it means that entry (i,4) of randQ is equal to 1 ; 如果此数组的第i个条目是(逻辑) 1 ,则意味着randQ条目(i,4)等于1 ;
  • if the ith entry of this array is a (logical) 0 , it means that entry (i,4) of randQ is not equal to 1 . 如果该数组的第i个条目是(逻辑) 0 ,则意味着randQ条目(i,4) 等于1

Array as a predicate 数组作为谓词

Now that you know that you're using an array for the predicate of the "if" statement, let's see what happens. 现在您知道您正在使用数组作为“if”语句的谓词,让我们看看会发生什么。 If the predicate of your "if" statement is an array, MATLAB will execute the "if" branch only if all entries of that array evaluate to logical 1 . 如果“if”语句的谓词是一个数组, 只有当该数组的所有条目都计算为逻辑1 ,MATLAB才会执行“if”分支。

For instance, 例如,

if [1, 2; 3, 4]
    disp("True")
else
   disp("False")
end

will display "True" , because all entries of [1,2;3,4] get cast as a logical 1 , which causes the predicate to be evaluated as logical 1 (true). 将显示"True" ,因为[1,2;3,4]所有条目都被转换为逻辑1 ,这导致谓词被评估为逻辑1 (真)。 However, 然而,

if [1, 2; 3, 0]
    disp("True")
else
   disp("False")
end

will display "False" , because entry (2,2) of the [1,2;3,0] gets cast as a logical 0 , which causes the predicate to be evaluated as logical 0 (false). 将显示"False" ,因为[1,2;3,0]条目(2,2)被转换为逻辑0 ,这导致谓词被评估为逻辑0 (假)。 Therefore, if at least one entry in randQ(:,4) is zero, the if will not get executed. 因此,如果randQ(:,4)中的至少一个条目为零, if不会执行if

Your assignment statements are incorrect anyway 无论如何,你的作业陈述都不正确

The assignment statement 作业陈述

randQ(:,4) = 0

would overwrite all entries in the 4th column with 0 , which is not what you want. 会用0覆盖第4列中的所有条目,这不是你想要的。

A better approach: use logical indexing 更好的方法:使用逻辑索引

You write 你写

I want to change all 1's into 0's and all 2's into 1's in column 4 in that 100-by-4 matrix. 我想在100 x 4矩阵的第4列中将所有1变为0,将所有2变为1。

A more idiomatic approach for this, as pointed out by giuseppe , is to use logical indexing : 正如giuseppe所指出的 ,更为惯用的方法是使用逻辑索引

randQ(randQ(:,4) == 1, 4) = 0;
randQ(randQ(:,4) == 2, 4) = 1;

No need to use the find function, though, because randQ(:,4) == 1 already returns what you want: a 100-by-1 logical array indicating which entries of the 4th column of randQ are equal to 1 . 但是,不需要使用find函数,因为randQ(:,4) == 1已经返回了你想要的东西:一个100×1的逻辑数组,指示第4列randQ哪些条目等于1

You are asking if your vector == 1 which is not the case. 你问的是你的向量== 1是不是这种情况。

There are more way to do it, but you can loop every value with a for. 有更多的方法可以做到这一点,但你可以使用for循环每个值。

The result of randQ(:,4) is a vector with a length of 100. You are then trying to compare it to a scalar, which will be true only if all the members of the vector (or matrix) are equal to the scalar. randQ(:,4)是一个长度为100的向量。然后你试图将它与一个标量进行比较,只有当向量(或矩阵)的所有成员都等于标量时才会成为标量。 。

> M=[1 1; 2 1; 3 2]
M =                                            

   1   1
   2   2
   3   1

> M(:,1)
ans =

   1
   2
   3

Also, the way you try to change the values isn't true, if you do randQ(:,4) = 1 then the whole 4 th column will get the value 1: 此外,您尝试更改值的方式不正确,如果您执行randQ(:,4) = 1那么整个 4列将获得值1:

>M(:,2)=0
M =

   1   0
   2   0
   3   0

The simplest way to achieve what you want is by iterating over the matrix in a loop row by row, checking the value and then changing it. 实现所需内容的最简单方法是逐行迭代循环中的矩阵,检查值然后更改它。

As also answered by the others you are trying to compare a vector with a scalar, that would never be true. 正如其他人所回答的那样,你试图将矢量与标量进行比较,这种情况永远不会成真。

Instead you can achieve your aim using the function find , this function in fact implements elements comparison. 相反,你可以使用函数find实现你的目标,这个函数实际上实现了元素比较。

randQ(find(randQ(:,4) == 1),4) = 0;
randQ(find(randQ(:,4) == 2),4) = 1;

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

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