查找矩阵中的值并将它们放入向量中
它一定很简单,但令人惊讶的是我无法在这里或通过反复试验找到这个问题的答案。
我想从矩阵中获取值(根据某些条件)并将这些值放入向量中。我还需要匹配值的下标索引。数据很多,所以 for 循环就不用了。
这是一个正确的(但迭代的)答案:
[I,J] = find(A > 5);
values = zeros(numel(I),1);
for i=1:numel(I)
values(i) = A(I(i),J(i));
end
我尝试了 values = A(I,J)
但这是 n×n 而不是 n×1。
It must be simple, but surprisingly I couldn't find an answer to this problem here or by trial-and-error.
I want to get values out of a matrix (according to some condition) and place the values into a vector. I also need the subscript indices of the matching values. There is a lot of data so for loops are out.
This is a correct (but iterative) answer:
[I,J] = find(A > 5);
values = zeros(numel(I),1);
for i=1:numel(I)
values(i) = A(I(i),J(i));
end
I tried values = A(I,J)
but this is n-by-n instead of n-by-1.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以隐式地将矩阵视为向量(线性索引< /a>):
请注意,您可以使用 逻辑索引:
You can implicitly treat the matrix like a vector (linear indexing):
Note that you can do this more efficiently with logical indexing: