不,无法覆盖
sparse
使用不同的值。尽管耗时耗力,但你能做的是
accumarray
:
x_ind; % I presume this to contain the column index of the number
y_ind; % I presume this to contain the row index of the number
value; % I presume this to contain the value (0 or 1)
new_mat = accumarray([x_ind y_ind],value,[],[],-1);
new_mat
现在将包含您的处方
0
和
1
值,并且具有
-1
在所有其他位置。您不必设置
size
参数(第三个),因为它只会创建一个矩阵
max(x_ind) x max(y_ind)
如果你放
[]
。第四个输入参数函数也可以为空,因为
x_ind
和
y_ind
将仅包含一个值,
mean
足够了。
例如:
A = [0 1 ; -1 0];
x_ind = [1;2;2];
y_ind = [1;1;2];
value = [0;1;0];
new_mat = accumarray([x_ind y_ind],value,[],[],-1);
new_mat =
0 1
-1 0
我更喜欢的另一种方法是简单地将所有值加一,从而生成1 2,并将0设置为1。这样,-1被映射为0,因此您可以使用
稀疏的
无论如何在示例中
A = [1 2;0 1]
可以使用
A-1
.
请注意:
稀疏的
为每个元素存储三个值(行、列、值),外加一些开销。所以如果你的矩阵小于70%,
稀疏的
实际上正在消耗
更多
比常规的、完整的矩阵更容易记忆。