Matlab中find()函数的逆函数是什么 [英] What is the inverse of find() function in matlab
本文介绍了Matlab中find()函数的逆函数是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Matlab中,我可以在矢量中找到所有非零条目,如下所示:
In Matlab I can find all non zero entries in a vector like this:
>> v = [0 1 0 0 1]
v =
0 1 0 0 1
>> indices = find(v)
indices =
2 5
假设我的向量v
只能具有0
和1
值,从indices
向量再现v
的简单方法是什么?
Assuming that my vector v
can only have 0
and 1
values, what is a simple way to reproduce v
from the indices
vector?
推荐答案
您必须知道v
的形状(即,如示例中的v
是多长的形状),但是一旦知道这很简单:
you have to know what the shape v
is (i.e. how long v
is if it's a vector as in your example), but once you know that it's trivial:
n = 5;
v_reconstructed = zeros(1, n);
v_reconstructed(indices) = 1;
如果您不知道v
多长时间,则不会捕获v
中最后一个1
之后的任何0
...
if you don't know how long v
is then you won't capture any 0
s after the last 1
in v
...
顺便说一句,如果您正在使用稀疏矩阵,那么您可能实际上希望这样做:
BTW if you are working with sparse matrices then you might want this actually:
v = sparse([0 1 0 0 1]);
v_reconstructed = full(v);
这篇关于Matlab中find()函数的逆函数是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文