使用八度来映射矢量值 [英] Using octave to map vector values

查看:134
本文介绍了使用八度来映射矢量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以解释如下代码:



Y(Y == 0)= -1 / p>

将所有值设置为0到-1。例如:



Y = [1 1 1 0 0 0 1]



我们得到:



Y = [1 1 1 -1 -1 -1 1]



令人困惑的是, Y == 0 不返回索引向量。
如果我尝试直接使用向量 Y == 0 我得到错误:



Y([0 0 0 1 1 1 0])= -1



错误:下标索引必须是正整数或逻辑值



我自然会选择:



Y(find(Y == 0))= -1



并且想知道为什么上述不使用 find



TIA

解决方案

这被称为逻辑索引。 gnu.org上有一些文档(请参阅 4.6逻辑值< a>),但是找到关于它的信息的最佳位置可能在 MATLAB文档



您的示例不起作用,因为您使用的是双精度数组,而不是逻辑数组,可以索引到 Y 。考虑以下(使用Octave 3.6.2):

 >> Y = [1 1 1 0 0 0 1] 
Y =

1 1 1 0 0 0 1

>> idx = Y == 0
idx =

0 0 0 1 1 1 0

>> idx_not_logical = [0 0 0 1 1 1 0]
idx_not_logical =

0 0 0 1 1 1 0

>> whos
当前范围内的变量:

Attr名称大小字节类
==== ==== ==== ===== =====
Y 1x7 56 double
ans 1x7 7 logical
cmd_path 1x489 489 char
gs_path 1x16 16 char
idx 1x7 7 logical
idx_not_logical 1x7 56 double

总共是533个元素,使用631个字节

>> Y(idx_not_logical)= - 1
错误:下标索引必须是正整数或逻辑值

>> Y(idx)= - 1
Y =

1 1 1 -1 -1 -1 1


Can anyone explain how the following code:

Y(Y==0) = -1

sets all values of 0 to -1. For example for:

Y = [1 1 1 0 0 0 1]

we get:

Y = [1 1 1 -1 -1 -1 1]

What is confusing me is that Y==0 does not return a vector of indices. An if I try to use the vector Y==0 directly I get the error:

Y([0 0 0 1 1 1 0]) = -1

error: subscript indices must be either positive integers or logicals

I would have naturally opted for:

Y(find(Y==0)) = -1

and would like to know why the above does not use find

TIA

解决方案

This is called logical indexing. There is some documentation on gnu.org (see 4.6 Logical Values), but the best place to find info about it is probably in the MATLAB documentation.

Your example does not work because you are using an array of doubles, instead of a logical array, to index into Y. Consider the following (using Octave 3.6.2):

>> Y = [1 1 1 0 0 0 1]
Y =

   1   1   1   0   0   0   1

>> idx = Y==0
idx =

   0   0   0   1   1   1   0

>> idx_not_logical = [0 0 0 1 1 1 0]
idx_not_logical =

   0   0   0   1   1   1   0

>> whos
Variables in the current scope:

   Attr Name                 Size                     Bytes  Class
   ==== ====                 ====                     =====  =====
        Y                    1x7                         56  double
        ans                  1x7                          7  logical
        cmd_path             1x489                      489  char
        gs_path              1x16                        16  char
        idx                  1x7                          7  logical
        idx_not_logical      1x7                         56  double

Total is 533 elements using 631 bytes

>> Y(idx_not_logical)=-1
error: subscript indices must be either positive integers or logicals

>> Y(idx)=-1
Y =

   1   1   1  -1  -1  -1   1

这篇关于使用八度来映射矢量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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