matlab中的二进制三维模糊矩阵

时间:2015-07-17 08:35:04

标签: algorithm matlab matrix

我有一个模糊矩阵M,其大小为3x3x3,以便

M(:,:,1)+M(:,:,2)+M(:,:,3)=matrix one %all elements is one
0<=M(:,:,i)<=1; i=1..3

我想将矩阵M转换为二进制矩阵,以便

if (M(p,q,i)>=M(p,q,j)) then M(p,q,i)=1, M(p,q,j)=0

其中p,q是元素的位置。我,j = 1..3

请注意,如果我们已设置M(p,q,i)=1,则M(p,q,j)必须为0(j = 1..3,j!= i)因为我想要二进制矩阵M的总和等于1(总和(M,3)= 1(3, 3))

你能帮我在matlab中表达这个想法吗?非常感谢你

例如:

M=zeros([3 3 3]);
M(:,:,1) =  [0.2000    0.3000    0.4000;
             0.5000    0.6000    0.7000;
             0.3000    0.2000    0.1000];


M(:,:,2) =  [0.4000    0.1000    0.6000;
             0.1000    0.3000    0.1000;
             0.1000    0.2000    0.1000];


M(:,:,3) =  [0.4000    0.6000         0;
             0.4000    0.1000    0.2000;
             0.6000    0.6000    0.8000];

我的预期输出是

M_out(:,:,1)=[0        0         0;
              1        1         1
              0        0         0];

M_out(:,:,2)=[1        0         1;
              0        0         0
              0        0         0];

M_out(:,:,3)=[0        1         0;
              0        0         0
              1        1         1];

3 个答案:

答案 0 :(得分:5)

bsxfun&amp; permute二人为genericvectorized解决方案解决了问题 -

%// Get max indices along dim-3
[~,idx] = max(M,[],3);

%// Setup o/p logical array with same size as M and 1s at starting max indices
M_out = bsxfun(@eq,idx,permute(1:size(M,3),[1 3 2]))

示例运行 -

>> M
M(:,:,1) =
          0.2          0.3          0.4
          0.5          0.6          0.7
          0.3          0.2          0.1
M(:,:,2) =
          0.4          0.1          0.6
          0.1          0.3          0.1
          0.1          0.2          0.1
M(:,:,3) =
          0.4          0.6            0
          0.4          0.1          0.2
          0.6          0.6          0.8
>> [~,idx] = max(M,[],3);
M_out = bsxfun(@eq,idx,permute(1:size(M,3),[1 3 2]))
M_out(:,:,1) =
     0     0     0
     1     1     1
     0     0     0
M_out(:,:,2) =
     1     0     1
     0     0     0
     0     0     0
M_out(:,:,3) =
     0     1     0
     0     0     0
     1     1     1

答案 1 :(得分:0)

这是一个适合您的解决方案。但是,如果typedef struct node { int data; struct node* next; } node; node* head; void append(node* pHead, int data) { node* current = pHead; node* newNode = NULL; newNode = (node*)malloc(sizeof(node)); newNode->data = data; newNode->next = NULL; if (current == NULL) pHead = newNode; else { while (current->next != NULL) current = current->next; } current->next = newNode; } int main(void) { head = NULL; int howMany; int num; printf("how many?"); scanf("%d", &howMany); for (int i = 0; i < howMany; i++) { printf("** %d ** number: ", i+1); scanf("%d", &num); append(head, num); } 增加,则代码太长

N

答案 2 :(得分:0)

这段代码几乎完全符合你的要求,唯一的区别是当max相同时(即M(p,q,i)== M(p,q,j))它会写一个1两个地点(i和j)。
这是代码:

M=zeros([3 3 3]);
M(:,:,1) =  [0.2000    0.3000    0.4000;
             0.5000    0.6000    0.7000;
             0.3000    0.2000    0.1000];


M(:,:,2) =  [0.4000    0.1000    0.6000;
             0.1000    0.3000    0.1000;
             0.1000    0.2000    0.1000];


M(:,:,3) =  [0.4000    0.6000         0;
             0.4000    0.1000    0.2000;
             0.6000    0.6000    0.8000];

output = zeros(size(M));
for i = 1:size(M,1)
    for j = 1:size(M,2)
        for k = 1:size(M,3)
            if M(i,j,k) == max(M(i,j,:))
                output(i,j,k) = 1;
            else
                output(i,j,k) = 0;
            end
        end
    end
end

请告诉我它是否适合您,或者如果在这两个地方放1都是您的问题。