Clear Filters
Clear Filters

Assign values from an matrix to another by using row and column indexes -- array exceeds maximum array size preference

5 views (last 30 days)
Hi
So I have an image (345*345 matrix), and I did this
[row, column] = find(image == 1);
i.e., to get the row and column indexes for all pixel values that is 1.
This is a binary image, so a lot if pixles have value 1. So the 'row' and 'column' each returns a 57033*1 size big array.
Now I want to assign a different values from another matrix to those pixel values that originally were 1. And they should be 1-1 matching from the two images using those indexes, something like below:
image(row,column) = image_new(row, column);
But then matlab returned an error:
Requested 57033x57033 (24.2GB) array exceeds maximum array size preference (16.0GB). This might cause MATLAB to become unresponsive.
Just wondering is there still a way to do what I propose to do. i.e., assign the values that were 1 from a new matrix in the same pixel positions? (you can think that the new matrix was like a foreground of the image to fill into the original binary image.)
Thanks in advance!

Answers (1)

Image Analyst
Image Analyst on 24 Nov 2023
You can just use the logical image. Don't use find or rows or columns at all. Simply do
mask = image1 == 1; % Logical image.
image1(mask) = image_new(mask)
  1 Comment
Dyuman Joshi
Dyuman Joshi on 24 Nov 2023
Logical values only take 1 byte per element, whereas double values take 8 byte per element. So the total memory costs will be cut by 7/8th
Not only that, logical indexing is much more faster than find().

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!