【问题标题】:Indexing patterns, determining function inputs matlab索引模式,确定函数输入 matlab
【发布时间】:2012-12-13 03:28:47
【问题描述】:

我有一个奇怪的问题,但这里是。我有一个包含(T11、T12、T21 和 T22)的矩阵...

| T11 T12 |
| T21 T22 |

我有一个函数,它会在比较矩阵中的两个值时返回一个标量值。让我们称之为f(x,y)。我想为每两个值运行这个函数,所以我这样做......

f(T11,T12), f(T11,T21), f(T11,T22),
f(T12,T21), f(T12,T22),
f(T21,T22)

我将这些结果存储在一个向量中,现在包含 6 个元素

V1 = [f(T11,T12), f(T11,T21), f(T11,T22), f(T12,T21), f(T12,T22), f(T21,T22)]

我现在要做的是找出哪个f(x,y) 对应于哪两个x,y 值。所以向量V1 将包含值,例如:

V1 = [12 14 54 23 86 3]

所以可以看到V1的第一个索引(i=1)的值为15,对应f(T11,T12),而V1的第三个索引(i=3)=54,对应@ 987654332@.

如果有任何不清楚的地方,请告诉我。为了刷新,我希望能够为V1 中的每个值确定输入到f(x,y) 的原始值。我已经尝试寻找模式,但到目前为止一直无法想出任何东西,我忘记了一堆我曾经知道的数学......我认为 V1 的指标和比较的指标之间存在关系矩阵。请出主意! (p.s. 我也尝试在函数返回中包含索引和值,但它真的很乱,我宁愿用一种奇特的数学方式来做。

【问题讨论】:

    标签: matlab


    【解决方案1】:

    啊,我们走了。我终于找到了解决这个棘手问题的方法!因此,您想从给定 V1 = index 中的索引的矩阵中找到原始值。我所做的是获取矩阵(m,n)的大小创建一个row=1, col=1,然后

    for i = 1 to (m*n) 
        if index - (m*n) plus i is <= 0, then col=index
        stop
    else
        index = index - mn + i
    row = row + 1
    

    现在您可以将矩阵展平为1 by m*n,您想要的两个值是rowrow+col

    【讨论】:

      【解决方案2】:

      我建议使用meshgrid 创建两个索引矩阵:

      N = numel(T);
      [ind1,ind2]=meshgrid(1:N);
      
      >>ind1 =
      
         1   2   3   4
         1   2   3   4
         1   2   3   4
         1   2   3   4
      
      >>ind2 =
      
         1   1   1   1
         2   2   2   2
         3   3   3   3
         4   4   4   4
      

      然后使用这些的严格上位或lower triangular partlinear index 转换为T

      selection = logical(tril(ones(N),-1)
      ind1 = ind1(selection);
      ind2 = ind2(selection);
      
      >>ind1' = 
         [1 1 1 2 2 3]
      >>ind2' = 
         [2 3 4 3 4 4]
      

      所以现在你可以简单地做:

      V1 = arrayfun(@f,T(ind1),T(ind2));
      

      现在ind1ind2 包含V1 中值的T 的行和列索引。

      我建议逐步执行这些代码行并检查中间变量以查看所有内容。

      【讨论】:

        【解决方案3】:

        在函数 f(x,y) 中使用的从矩阵中取出元素的顺序(假设它被命名为 A,并且具有 m 行和 n 列的维度),就像你正在查看向量(假设 m = 2,A(1,:) 是矩阵 A 的第一行)

        w = [ A(1,:) A(2,:)]
        

        并通过获取向量的最左侧元素并将其与右侧的所有元素进行比较(从左到右的顺序)来获取其所有组合(假设矩阵 A 中的所有条目都不相同) ,依此类推,直到您从向量 w 生成所有大小为 2 的组合。 I 矩阵 A 定义如下

        A = [ 1 2; 3 4]
        

        生成的 A 的条目大小为 2 的组合将是(注意组合表示为以下矩阵 B 的行)

        B = [1 2; 1 3; 1 4; 2 3; 2 4; 3 4]
        

        我们知道矩阵 A 中有 m*n 个条目(在本例中为 4)。在您问题的向量 V1 中,我们知道前 (m*n - 1) 个条目都将向量 w 的第一个元素作为 f(x,y) 的第一个输入,而下一个 (m*n - 2 ) 条目将向量 w 的第二个元素作为 f(x,y) 的第一个输入,依此类推。如果 index 等于 V1 中的索引,下面的代码应该会给你答案

        p = 1
        for i = (m*n - 1):-1:1
          for j = 1:i
            index = index - 1
            if (index == 0)
              disp('The following number is the position of x in vector w')
              disp(p)
              pw = p
              disp('The following number is the position of y in vector w')
              disp(p + j)
              qw = p + j
          p = p + 1
        
        disp('The row of input x of f(x,y) in A is the following:')
        disp(idivide(int32(pw - 1),n))
        disp('The column of input x of f(x,y) in A is the following:')
        disp(pw - (idivide(int32(pw - 1),n)*n))
        disp('The row of input y of f(x,y) in A is the following:')
        disp(idivide(int32(qw - 1),n))
        disp('The column of input y of f(x,y) in A is the following:')
        disp(qw - (idivide(int32(qw - 1),n)*n))
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-16
          • 1970-01-01
          • 2013-01-05
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多