2

I want to create a matrix without using any loop, like in the following program I made in MATLAB 2011a for forming matrix n(x,y).

segment1 and segment2 have the same dimensions (you can take any two matrices).

segment1 = [1 2 3;4 5 6];
segment2 = [5 2 6;9 1 2];

seg1_max = max(max(segment1));
seg2_max = max(max(segment2));

n = zeros(seg1_max, seg2_max);
i = 1; j = 1;

while i<=size(segment1, 1)        
    while j<=size(segment1, 2)    
        x = segment1(i, j);
        y = segment2(i, j);
        n(x,y) = n(x,y)+1;
        j = j+1;    
    end;
i = i+1; j = 1;
end;

I have also made this program using for loop, but i want to get matrix n(x,y) without using loop operations.

1 Answer 1

2

The function accumarray can be used for this. Given your segment1 and segment2, the following computes m, which will be the same as your n:

x = segment1(:);
y = segment2(:);
m = accumarray([x y], ones(size(x)));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.