矩阵的边界值问题 [英] Issue in bounding values of matrix
问题描述
问题:我需要将P矩阵的每个元素限制在[-1,1]范围内.我已经使用了代码 MinMaxCheck 提供,但是此代码仅适用于向量.因此,我对其进行了修改以适用于矩阵.修改后的功能代码MinMaxCheckMatrix
确实仅更改了第一列,但未检查也不限制第二列元素.我无法理解为什么整个P矩阵都没有受到影响.请帮助纠正.
Problem : I need to clamp each of the elements of P matrix in the range [-1,1]. I have used the code MinMaxCheck provided but this code works only for vectors. So, I have modified it to work for matrix. The modified function code MinMaxCheckMatrix
does change only the first column but the second column elements are not checked nor clamped. I am unable to understand why the entire P matrix is not getting affected. Please help in rectifying.
SomeVector = [20 0
-20 1
5 0.9
1.1 0.2
10 20];
for rows =1:5
P(rows,:) = SomeVector;
min_range(rows,:)=-1;
max_range(rows,:)=1;
end
P = MinMaxCheckMatrix(min_range, max_range, P);
**Function**
function [ A2comp ] = MinMaxCheckMatrix( minimum, maximum, A2comp )
[row col] = size(minimum);
for i=1:row
for j=1:col
if(maximum(i,j)<A2comp(i,j)||minimum(i,j)>A2comp(i,j))
if(maximum(i,j)<A2comp(i,j))
A2comp(i,j)=maximum(i,j);
else A2comp(i,j)=minimum(i,j);
end
end
end
end
This is the result that I got:
P =
1.0000 0
-1.0000 1.0000
1.0000 0.9000
1.0000 0.2000
1.0000 20.0000
推荐答案
除非我丢失了某些东西,否则看来这是一种非常过于复杂的方法:
Unless I'm missing something, that seems like a very overcomplicated way to do this:
P(P < -1) = -1;
P(P > 1) = 1;
或更狂热的人:
function mat = clamp(mat, minval, maxval)
mat(mat < minval) = minval;
mat(mat > maxval) = maxval;
这篇关于矩阵的边界值问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!