【发布时间】:2019-04-13 17:25:21
【问题描述】:
我有一个像 x = [0 0 1 1 1 1 1 1 1] 这样的二进制向量。我想找到让我们说第 7 个 1,即 9 的索引。
我知道我能做到:
y = find(x);
index = y(7);
但是如果向量很大并且我想节省内存使用量怎么办? y = find(x) 不会占用大量内存吗?如果是这样,有没有办法解决这个问题?
我将其用作在线性规划问题中存储非基元素和基元素的索引的替代方法。所以我想避免将索引存储为数值。
以下是一个好的解决方案吗?
basis = [0 0 1 1 1 1 1 1 1];
basisIndex = 7;
correctIndex = getIndex(basisIndex, basis); % should be 9 when basisIndex = 7
function ret = getIndex(basisIndex, basis)
counter = 1;
for value = find(basis) % iterate through [3, 4, 5, 6, 7, 8, 9]
if counter == basisIndex
ret = value;
break;
end
counter = counter + 1;
end
end
【问题讨论】:
-
我在问题的最后添加了一些信息。我在想保存 find 返回的值会带走将索引存储在二进制向量中的优势。
-
@SardarUsama 我在想我添加的功能会节省内存?
标签: matlab vector indexing binary