matlab用1和0替换行中矩阵中的元素

发布于 2025-01-20 09:52:08 字数 110 浏览 4 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月亮是我掰弯的 2025-01-27 09:52:08

对于元素的两个相同大小矩阵的比较,请使用“ >”运算符EG result = data>阈值(这将根据是否满足条件返回和零)。

假设您将数据放在一个名为data的矩阵中,并且在列中的阈值称为thresholds(即length(thresholds)== size(data,1))。您可以使用repmatthresholdsmatrix = repmat(thresholds,1,size(data,2))创建与数据矩阵相同的数组大小。
然后,您可以将其与您的数据进行比较:

result = data> repmat(阈值,1,大小(数据,2))
这应该给您想要的结果。

[请注意,您还可以直接将向量与矩阵进行比较,而无需使用repmat ie result = 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 called thresholds (i.e. length(thresholds) == size(data, 1)). You can create an array the same size as the data matrix using repmat: 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]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文