Asked by Zoltán Csáti
on 18 Oct 2015

I have a matrix in which I change only one element in every iteration of a loop. I want to display that matrix as an image using image or imagesc. What I am doing now is to update the CData property of the image handle so as to refresh the image of the matrix. With several thousands of iterations, it turned out that the bottleneck is the image/imagesc function according to the Profiler. So I thought of changing the color of the tile corresponding to the changed matrix entry. How can I do that? There would be no problem with the colormap since the matrix only contains zeros and ones.

Answer by Image Analyst
on 18 Oct 2015

Accepted Answer

Don't use image. Use set(). See this demo:

grayImage = imread('moon.tif');

hImage = imshow(grayImage);

promptMessage = sprintf('Do you want to assign new pixels,\nor Quit?');

titleBarCaption = 'Continue?';

buttonText = questdlg(promptMessage, titleBarCaption, 'Continue', 'Quit', 'Continue');

if strcmpi(buttonText, 'Quit')

return;

end

for k = 1 : 50 : numel(grayImage);

grayImage(k) = 255; % Make this pixel bright white.

set(hImage, 'CData', grayImage);

end

msgbox('Done');

I think it should be fast enough for you.

Zoltán Csáti
on 19 Oct 2015

Image Analyst
on 19 Oct 2015

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 9 Comments

## Matt J (view profile)

## Direct link to this comment

https://es.mathworks.com/matlabcentral/answers/249276-how-to-efficiently-update-an-image-if-one-matrix-element-is-changed#comment_317010

## Jan (view profile)

## Direct link to this comment

https://es.mathworks.com/matlabcentral/answers/249276-how-to-efficiently-update-an-image-if-one-matrix-element-is-changed#comment_317018

## Zoltán Csáti (view profile)

## Direct link to this comment

https://es.mathworks.com/matlabcentral/answers/249276-how-to-efficiently-update-an-image-if-one-matrix-element-is-changed#comment_317023

## dpb (view profile)

## Direct link to this comment

https://es.mathworks.com/matlabcentral/answers/249276-how-to-efficiently-update-an-image-if-one-matrix-element-is-changed#comment_317033

## Zoltán Csáti (view profile)

## Direct link to this comment

https://es.mathworks.com/matlabcentral/answers/249276-how-to-efficiently-update-an-image-if-one-matrix-element-is-changed#comment_317035

## dpb (view profile)

## Direct link to this comment

https://es.mathworks.com/matlabcentral/answers/249276-how-to-efficiently-update-an-image-if-one-matrix-element-is-changed#comment_317039

## Zoltán Csáti (view profile)

## Direct link to this comment

https://es.mathworks.com/matlabcentral/answers/249276-how-to-efficiently-update-an-image-if-one-matrix-element-is-changed#comment_317066

## Walter Roberson (view profile)

## Direct link to this comment

https://es.mathworks.com/matlabcentral/answers/249276-how-to-efficiently-update-an-image-if-one-matrix-element-is-changed#comment_317095

## dpb (view profile)

## Direct link to this comment

https://es.mathworks.com/matlabcentral/answers/249276-how-to-efficiently-update-an-image-if-one-matrix-element-is-changed#comment_317108

Sign in to comment.