Matlab中find()函数的逆函数是什么 [英] What is the inverse of find() function in matlab

查看:97
本文介绍了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只能具有01值,从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 0s 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆