# What does the function 'ones' do to a matrix when trying to load an image?

1 view (last 30 days)
Alexandar on 8 Jul 2022
Answered: Image Analyst on 9 Jul 2022
I am just wondering why we need to put the ones for this code to work. When I get rid of the ones it does not work. Can I get an explanation of why this is?
BrainImage = ones(91, 109,'int8');
##### 2 CommentsShowHide 1 older comment
Alexandar on 8 Jul 2022
Hello Matt, sorry about that. I did not inlcude the rest of my code. My question is about the basic understanding of why we do this for imaging. Here is the rest of the code you can look at.
% Relies on https://www.fieldtriptoolbox.org/template/atlas/ and
% https://github.com/fieldtrip/fieldtrip/tree/master/template/atlas and
% https://www.gin.cnrs.fr/en/tools/aal/
BrainImage = ones(91, 109,'int8'); %creates the variable for the brain image to be read
BrainImage(:,:) = aalAtlas.tissue(:, :, 50) %replaces the one with the tissue is part of the 3D BrainImage of the Atlas that looks at tissue
test_size1 = size(app.CerebralAreasListBox.Value,2) %take into account what is selected, looks at columns only
area_num = ones(1,test_size1) %Changes the # of values selected to a row of that same # of 1's
for j=1:test_size1 % Cycle through the values you have choosen on the list
area_num(j) = str2double(app.CerebralAreasListBox.Value(j));
end
test_size2 = size(app.CerebralAreasListBox_2.Value,2) %take into account what is selected, looks at columns only
area_num2 = ones(1,test_size2)
for j=1:test_size2 % Cycle through lateralization, step change the multiplier
area_num2(j) = str2double(app.CerebralAreasListBox_2.Value(j));
end
test_size3 = size(app.CerebralAreasListBox_3.Value,2) %take into account what is selected, looks at columns only
area_num3 = ones(1,test_size3)
for j=1:test_size3 % Cycle through lateralization, step change the multiplier
area_num3(j) = str2double(app.CerebralAreasListBox_3.Value(j));
end
%put it in a for loop to display each section
for slice = 1:91
BrainImage(:,:) = aalAtlas.tissue(:, :, slice);
ColorBrainImage = ind2rgb(BrainImage, copper);
regions_1 = ismember(BrainImage, area_num);
ColorBrainImage(regions_1) = uint8(2* double(ColorBrainImage(regions_1)));
regions_2 = ismember(BrainImage, area_num2);
ColorBrainImage(regions_2) = uint8(3* double(ColorBrainImage(regions_2)));
regions_3 = ismember(BrainImage, area_num3);
ColorBrainImage(regions_3) = uint8(4* double(ColorBrainImage(regions_3)));
BBrainImage = imresize(ColorBrainImage,9);
imshow(BBrainImage)
end

GandaBerunda on 8 Jul 2022
Hi Alexandr,
ones() returns an array or a matrix all the elements of which are the number 1. In your case, it will return a 91x109 int8 array all of whose elements are 1, which will be stored in BrainImage.

Image Analyst on 9 Jul 2022
If you don't, and you just do
BrainImage(:,:) = aalAtlas.tissue(:, :, 50)
then, since ":" means "all" it will try to take the 50th slice and put it into BrainImage. The problem is that since you used : it means 1:end but it doesn't know the size of BrainImage yet so it throws an error with that syntax. It would have worked if you just didn't try to reference any row and column range, like this:
BrainImage = aalAtlas.tissue(:, :, 50)

### Categories

Find more on Image Arithmetic in Help Center and File Exchange

R2022a

### Community Treasure Hunt

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

Start Hunting!