matlab用1和0替换行中矩阵中的元素
我在MATLAB中有一个NXN矩阵。在每个行中,如果每个元素中的值高于某个阈值,则将该元素替换为1。else,用0替换。 注意:在每一行中,我们都以不同的阈值比较元素的值。
I have an n x n matrix in MATLAB. In every row, if the value in each element is higher than a certain threshold, replace that element with a 1. Else, with a 0.
NOTICE: In every row, we compare the value of element with different threshold.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于元素的两个相同大小矩阵的比较,请使用“
>
”运算符EGresult = data>阈值
(这将根据是否满足条件返回和零)。假设您将数据放在一个名为
data
的矩阵中,并且在列中的阈值称为thresholds
(即length(thresholds)== size(data,1)
)。您可以使用repmat
:thresholdsmatrix = repmat(thresholds,1,size(data,2))
创建与数据矩阵相同的数组大小。然后,您可以将其与您的数据进行比较:
result = data> repmat(阈值,1,大小(数据,2))
。这应该给您想要的结果。
[请注意,您还可以直接将向量与矩阵进行比较,而无需使用
repmat
ieresult = data>阈值
,但是IMO可能不清楚,可能导致意外行为]For element-wise comparison of two matrices of the same size, use the "
>
" operator e.g.result = data > threshold
(this will return ones and zeroes depending on whether the condition is satisfied or not).Suppose you have your data in a matrix called
data
and your thresholds in a column vector calledthresholds
(i.e.length(thresholds) == size(data, 1)
). You can create an array the same size as the data matrix usingrepmat
:thresholdsMatrix = repmat(thresholds, 1, size(data, 2))
.You can then compare this to your data:
result = data > repmat(thresholds, 1, size(data, 2))
.This should give you the result you want.
[Note that you can also directly compare the vector to the matrix without using
repmat
i.e.result = data > thresholds
, but IMO this can be unclear and may lead to unexpected behaviour]