本文介绍了转换成在MATLAB逻辑阵列整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
欲的整数 I
转换为一个逻辑矢量与第i个非零元素。可与DE做 1:10 == 2
,返回
I want to convert an integer i
to a logical vector with an i-th non-zero element. That can de done with 1:10 == 2
, which returns
0 1 0 0 0 0 0 0 0 0
现在,我想向量化这个过程的每一行。写 repmat(1:10,2,1)== [2〜5]
我期望能获得
Now, I want to vectorize this process for each row. Writing repmat(1:10, 2, 1) == [2 5]'
I expect to get
0 1 0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0
但是,相反,出现此错误:
But instead, this error occurs:
使用错误==结果
矩阵的尺寸必须一致。
我可以向量化这个过程中,或者是为
循环的唯一选择?
Can I vectorize this process, or is a for
loop the only option?
推荐答案
您可以使用 bsxfun
:
>> bsxfun(@eq, 1:10, [2 5].')
ans =
0 1 0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0
请注意转置
第二矢量。这是非常重要的。
Note the transpose .'
on the second vector; it's important.
这篇关于转换成在MATLAB逻辑阵列整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!