为什么这个MATLAB'if'语句不起作用? [英] Why does this MATLAB 'if' statement not work?

查看:1885
本文介绍了为什么这个MATLAB'if'语句不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是MATLAB的新手,我无法弄清楚为什么我的if语句和条件不起作用。我有一个100乘4的矩阵( randQ )。列 1 3 是随机整数,第4列包含每行1或2。

I'm pretty new to MATLAB and I can't figure out why my "if" statement and condition don't work. I have a 100-by-4 matrix (randQ). Columns 1 through 3 are random integers, and column 4 contains either a 1 or 2 in each row.

我想将所有 1 改为 0 和所有 2 进入 1 4 在那个100乘4的矩阵中。
我正在做的是:

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. 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:

  • you are trying to compare a vector with a scalar, that would never be true.
  • You are asking if your vector == 1 which is not the case.

例如,

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

将显示真正的。请参阅下面的解释。

will display "True". See below for an explanation.

您写的


我无法弄清楚为什么我的if陈述和条件不起作用

I cant figure out why my "if" statement and condition does not work

发生的事实实在是非常微妙。你需要了解几件事:

What's happening is actually quite subtle. You need to develop an understanding of several things:


  1. 表达式 randQ(:,4)== 1 返回,

  2. 当出现谓词时,if语句的作用是什么,不是标量,而是数组,

  3. 什么 randQ(:,4)= 0 呢。

  1. what the expression randQ(:,4) == 1 returns,
  2. what the "if" statement does when presented with a predicate that is, not a scalar, but an array,
  3. what randQ(:,4) = 0 does.

此外,您需要开始使用 逻辑索引

Furthermore, you need to start using logical indexing for this kind of operations.

假设 randQ 是一个100乘4的数组,表达式 randQ(:,4)== 1 返回100乘1 逻辑数组,即完整的(逻辑)0和1:

Under the assumption that randQ is a 100-by-4 array, the expression randQ(:,4) == 1 returns a 100-by-1 logical array, i.e. full of (logical) zeros and ones:


  • 如果此数组的第i个条目是(logica l) 1 ,这意味着 randQ 的条目(i,4)等于 1 ;

  • 如果此数组的第i个条目是(逻辑) 0 ,则表示 randQ 的条目(i,4)等于 1

  • if the ith entry of this array is a (logical) 1, it means that entry (i,4) of randQ is equal to 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.

现在你知道你正在使用数组对于if语句的谓词,让我们看看会发生什么。如果if语句的谓词是一个数组,只有当该数组的所有条目都计算为逻辑 1 时,MATLAB才会执行if分支

Now that you know that you're using an array for the predicate of the "if" statement, let's see what happens. 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, 2; 3, 4]
    disp("True")
else
   disp("False")
end

将显示True,因为的所有条目[1,2; 3,4] ] 将转换为逻辑 1 ,这会导致谓词被评估为逻辑 1 (真)。但是,

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). However,

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

将显示False,因为的条目(2,2)[1, 2; 3,0] 被转换为逻辑 0 ,这会导致谓词被评估为逻辑 0 (false)。因此,如果 randQ(:,4)中的至少一个条目为零,则 if 将不会被执行。

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). Therefore, if at least one entry in randQ(:,4) is zero, the if will not get executed.

作业陈述

randQ(:,4) = 0

会用 0 覆盖第4列中的所有条目,这不是你想要的。

would overwrite all entries in the 4th column with 0, which is not what you want.

你写


我想将所有1改为0并且在第4列的所有2中都是1乘100×4矩阵。

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.

更为惯用的方法,指出的https://stackoverflow.com/a/26700402/2541573>是使用 逻辑索引

A more idiomatic approach for this, as pointed out by giuseppe, is to use logical indexing:

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

无需使用 find 函数,但是,因为 randQ(:,4)== 1 已经返回了你想要的东西:一个100×1的逻辑数组,表明<$ c的第4列的哪些条目$ c> randQ 等于 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.

这篇关于为什么这个MATLAB'if'语句不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆