有没有办法在不使用循环的情况下对矩阵进行4个单元格计算

时间:2016-12-10 11:10:48

标签: matlab matrix

假设我有两个m×n矩阵ABB是零\一二进制矩阵。对于(i,j)中包含1的每个单元B,我想进行以下计算:

(A(i,j)sign(A(i+1,j))-A(i+1,j)sign(A(i,j)))/(sign(A(i+1,j))-sign(A(i,j)))

  • 这可以在没有循环的情况下完成吗?
  • 是否有卷积过滤器可以让我这样做?
  • 如果不是最好的方法来实现这个?

1 个答案:

答案 0 :(得分:1)

您可以直接使用B来索引矩阵A中要进行计算的位置。更难的部分来自于您需要在i+1处获取值。

如果你在B的最后一行中有1个值,那么你去下面一行的值可能会有问题,我将在下面假设你没有。如果你这样做,只需处理这个案例并将0'放在B的最后一行。

% Get the indexes from your matrix B :
Indexes=find(B(:));

% With how indexing works in MATLAB, you can fetch the value in the row below the fetched index by just adding 1 to the indexes :
% Note : This is where it will mess up if you have 1's in the last row of `B`
Indexes_Next=Indexes+1;

% Do your calculation and fill them in a matrix `C`, using the indexes :
C=zeros(size(A));
C(Indexes)=A(Indexes).*sign(A(Indexes_Next))-A(Indexes_Next).*sign(A(Indexes))./sign(A(Indexes_Next))-sign(A(Indexes));

玩具示例:

m=20;
n=10;

B=randi(2,m,n)-1;
B(end,:)=0;

B =

   1   1   1   0   1   0   0   0   1   1
   1   1   1   0   0   0   1   1   1   0
   1   0   1   0   1   0   0   1   1   1
   1   0   1   1   1   0   0   1   1   0
   0   1   0   0   1   1   0   0   0   0
   1   1   0   1   0   1   0   1   1   0
   1   1   1   1   1   0   0   0   1   1
   1   0   1   1   0   0   1   0   1   0
   1   0   0   0   1   1   0   0   0   0
   1   0   0   0   0   1   1   0   1   1
   1   0   0   1   1   1   0   0   1   1
   1   0   0   1   0   1   1   0   0   0
   0   0   0   0   0   1   0   0   1   1
   1   0   1   0   0   0   0   1   0   0
   0   1   1   1   1   0   1   0   0   1
   1   0   1   1   1   0   0   0   0   1
   1   0   1   0   0   1   1   1   1   0
   0   0   0   0   0   1   0   1   1   0
   0   1   0   1   1   0   1   1   0   0
   0   0   0   0   0   0   0   0   0   0

A=randn(m,n);

A =

 Columns 1 through 6:

  -1.7288795  -0.5695908   0.4039973   0.4178222   1.9367721  -0.6211859
  -0.4945766  -0.2536017   1.0269974   0.5980587   0.1747083   0.8528545
   0.9100027   0.9414117   1.6132723   0.2620914  -1.1753700   0.1701842
  -0.5094579  -1.5272232  -0.0270926   1.1561264  -0.8968151   0.5069460
  -0.4602732  -0.2735907  -0.0827892   0.1235966   0.2566247  -0.6536810
   0.0021322   0.2238454  -1.9115733   1.0635302  -0.9725437   0.9375332
   1.1415043   0.7147173   1.1052473  -1.0797606  -1.0508984  -0.8286737
  -1.1394488  -0.9882800   0.8274943   0.5247194   0.1164014   1.6475021
  -0.6300175  -0.2726597   1.9213140  -0.2248069   0.8301544   1.2854954
   0.9846066  -0.0560177  -0.1916319   0.8563492   0.1493325  -1.1080141
   0.8041186   0.3818663  -0.6250196   0.4691516   1.6304691   1.9712590
   0.3986143   0.2072083   0.5478786   0.8156810   0.7733391   0.1741200
   2.1892863  -0.6791961  -0.5672075  -1.2172510  -2.1611237   0.2908826
  -0.0317070   0.7387466  -0.8868863  -1.6897724   0.0445329  -0.7162208
   0.7670334   0.1991324  -1.8576145  -0.9314224   0.3317805   2.3690729
  -0.2091770   2.1657798  -1.0850389  -0.5931122  -1.6465039  -1.8521626
  -0.7298015   0.5714788  -1.9456336   2.5631989   0.8980204   0.7283266
   0.0126946  -2.0152983   0.0837190   0.0824572  -1.9949677  -0.7532233
   0.6209000  -1.5505561  -0.0273443  -0.0888351   0.3024363   0.0104319
   0.1721166  -0.9743276   2.1976957  -0.5196333   0.4917723   0.5235348

 Columns 7 through 10:

  -1.3386919   1.5511076  -0.0823747   0.6598207
   0.1000738   1.3927819   1.2658383   0.3986191
  -0.3424243  -1.3382283  -0.9360141   1.3003693
  -0.6647429  -0.4520907  -0.6012909   0.7016295
  -1.2671007   1.0972997  -0.6613468   0.2677242
  -0.7083392   0.8622831   1.1582403   0.7597853
   0.3218546   1.7128733   0.6578481  -1.7975630
  -0.1816331   0.1582915   0.0125391  -1.6381652
   1.1860973  -0.2897986   0.9418221   0.5233429
   0.9335806   0.2088674  -1.1177073  -0.3280461
   0.8333193   0.7273941  -0.6683300  -0.6137806
   0.0360114  -0.3457279  -0.9115280  -0.6171204
  -1.2861883  -0.7169084  -0.4313750  -0.4534017
  -0.3267513  -0.3327733   1.0649930   0.9503430
   0.3513518   0.7668540   0.8704655   0.3367261
   0.6942558  -0.0811757  -0.0930538   0.4074495
   0.0882976  -0.2911365  -3.0432331  -0.3264093
   0.4319805  -0.3641444   0.3984263  -0.0481398
   0.8404379  -0.4123483  -0.0465423  -0.1831455
  -0.8230964   0.3130749   0.6854873  -0.2411392

Output :
--------

 C
C =

 Columns 1 through 8:

   3.22346   1.82319  -1.62300   0.00000   0.76206   0.00000   0.00000   0.00000
   1.41543   1.68781  -1.58627   0.00000   0.00000   0.00000  -1.44250  -3.73101
  -2.41946   0.00000  -2.64036   0.00000   3.07219   0.00000   0.00000   2.79032
   1.96973   0.00000   1.10988   0.03253   0.35981   0.00000   0.00000   1.64521
   0.00000   0.95025   0.00000   0.00000  -2.22917   1.28385   0.00000   0.00000
  -2.13937  -1.49087   0.00000  -3.14329   0.00000  -2.76621   0.00000  -1.85059
  -3.28095  -2.70300  -0.72225   0.44496   0.06550   0.00000   0.00000   0.00000
   2.76947   0.00000  -2.09382  -1.74953   0.00000   0.00000   2.00446   0.00000
   1.35459   0.00000   0.00000   0.00000  -0.31918  -3.39351   0.00000   0.00000
  -0.81951   0.00000   0.00000   0.00000   0.00000   1.86324  -0.89974   0.00000
  -0.59450   0.00000   0.00000  -1.34653  -0.14287   0.79714   0.00000   0.00000
  -2.79067   0.00000   0.00000  -3.03293   0.00000  -1.11676  -2.32220   0.00000
   0.00000   0.00000   0.00000   0.00000   0.00000  -2.00710   0.00000   0.00000
   1.73533   0.00000   3.74450   0.00000   0.00000   0.00000   0.00000   1.43408
   0.00000  -2.96665   3.94265   2.52453  -2.97828   0.00000  -1.34290   0.00000
   1.93898   0.00000   4.03067   2.97009   0.25152   0.00000   0.00000   0.00000
   0.28289   0.00000  -0.86191   0.00000   0.00000  -2.48155  -1.34368   1.65528
   0.00000   0.00000   0.00000   0.00000   0.00000   0.25721   0.00000   1.77649
   0.00000   3.52488   0.00000   1.60847  -1.18934   0.00000  -2.66353   0.90073
   0.00000   0.00000   0.00000   0.00000   0.00000   0.00000   0.00000   0.00000

 Columns 9 and 10:

   2.18346  -0.73880
  -3.20185   0.00000
   2.53731  -0.40126
   2.26264   0.00000
   0.00000   0.00000
  -0.49961   0.00000
  -0.35469   4.43573
  -1.92928   0.00000
   0.00000   0.00000
   2.78604   1.94183
   2.57986   2.23090
   0.00000   0.00000
   1.63362   1.49694
   0.00000   0.00000
   0.00000  -1.07072
   0.00000  -1.73386
  -1.64481   0.00000
  -1.44497   0.00000
   0.00000   0.00000
   0.00000   0.00000