我在MATLAB中有一个n×n矩阵。在每一行中,如果每个元素中的值高于某个阈值,则将该元素替换为1。否则,替换为0。注意到:在每一行中,我们比较了不同阈值的元素的值。
发布于 2022-04-09 23:30:03
对于两个大小相同的矩阵的元素比较,使用">
“运算符(例如result = data > threshold
)(这将根据条件是否满足返回1和0)。
假设数据位于名为data
的矩阵中,阈值位于名为thresholds
的列向量中(即length(thresholds) == size(data, 1)
)。可以使用repmat
:thresholdsMatrix = repmat(thresholds, 1, size(data, 2))
创建与数据矩阵大小相同的数组。然后您可以将其与您的数据进行比较:
result = data > repmat(thresholds, 1, size(data, 2))
。这应该会给你你想要的结果。
请注意,您也可以直接将向量与矩阵进行比较,而无需使用repmat
即result = data > thresholds
,但这可能不清楚,并可能导致意外行为。
https://stackoverflow.com/questions/71813963
复制