简体   繁体   English

在MATLAB中根据另一个矩阵值复制矩阵值

[英]copying matrix values based on another matrix value in MATLAB

Hi I am trying to perform this on MATLAB 嗨,我正在尝试在MATLAB上执行此操作

A = A =

64     2     3    61    60     6     7    57
 9    55    54    12    13    51    50    16
17    47    46    20    21    43    42    24
40    26    27    37    36    30    31    33
32    34    35    29    28    38    39    25
41    23    22    44    45    19    18    48
49    15    14    52    53    11    10    56
 8    58    59     5     4    62    63     1

I want to select values from A based on F 我想根据F从A中选择值

F = F =

 0     0     0     0     0     0     0     0
 0     0     0     0     0     0     0     0
 0     0     0     0     0     0     0     0
-1    -1    -1    -1    -1    -1    -1    -1
 0     0     0     0     0     0     0     0
-1    -1    -1    -1    -1    -1    -1    -1
-1    -1    -1    -1    -1    -1    -1    -1
-1    -1    -1    -1    -1    -1    -1    -1

I want this output 我想要这个输出

u = u =

40    26    27    37    36    30    31    33
41    23    22    44    45    19    18    48
49    15    14    52    53    11    10    56
 8    58    59     5     4    62    63     1

This means that I want all the values corresponding to '-1' in a matrix. 这意味着我想要矩阵中与“ -1”对应的所有值。 i tried u=A(F==-1) ... but it gives me a single column with all the values like this: 我试过了u = A(F ==-1)...但是它给了我一列所有这样的值:

u = u =

40
41
49
 8
26
23
15
58
27
22
14
59
37
44
52
 5
36
45
53
 4
30
19
11
62
31
18
10
63
33
48
56
 1

This will do it:- 这样做:

u=-A.*F;               
u(all(u==0,2),:)=[]    %Removing rows containing zeros

Another solution:- 另一个解决方案:

u=reshape(A(F==-1),4,8) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM