J L
J L

Reputation: 2297

How to draw/plot with multiple matrices

Hi I am trying to draw an image.

I have three matrices:

Matrix A: X coordinates

Matrix B: Y coordinates

Matrix C: Image gray scale

For example:

A = [1, 1;     B = [1, 2;       C = [1, 2;
     2, 2]          1, 2]            3, 4]

I will plot a point with value of C(1) at X(1), Y(1). Value 1 is drawn at (1,1) Value 2 is drawn at (1,2) Value 3 is drawn at (2,1) Value 4 is drawn at (2,2)

Is there a function that I can use to plot this, or do I have to implement this? Any suggestion how to implement this would be appreciated it. Thank you.

Upvotes: 0

Views: 187

Answers (1)

Matthew Wesly
Matthew Wesly

Reputation: 1238

Is it a full image? And A, B, and C are 1D, right? If so you could make a 2D array with the values of Matrix C at the corresponding indices, convert it to an image and display the images.

img = zeros(max(max(B)),max(max(A)));   %initialize the new matrix
for i = 1:numel(C)                      %for each element in C
        img(B(i),A(i)) = C(i);          %fill the matrix one element at a time
end
img = mat2gray(img);                    %optional. More information in edit
imshow(img);                            %display the image

This assumes that the minimum index value is 1. If it is 0 instead, you'll have to add 1 to all of the indices.

My matlab is a little rusty but that should work.

edit: Is there any reason why they are two dimensional arrays to start? Regardless, I've updated my answer to work in either case.

edit2: mat2gray will scale your values between 0 and 1. If your values are already grayscale this is unnecessary. If your values range another scale but do not necessarily contain the min and max values, you can specify the min and max. For example if your range is 0 to 255, use mat2gray(img,[0,255]);

Upvotes: 0

Related Questions