How to solve the problem?
Show older comments
I have a 198x198 matrix, an image. I need to get 3x3 matrices each from the above matrix. Total, 22 matrices, how to access each of them? can somebody help me with the code??
Answers (1)
Walter Roberson
on 26 Apr 2018
blocks = mat2cell(YourMatrix, 3*ones(1,22), 3*ones(1,22));
then you would access (for example) blocks{7,4) to give you the 7th block of 3 down, 4'th block of 3 across.
17 Comments
Darsana P M
on 26 Apr 2018
Walter Roberson
on 26 Apr 2018
You said before your arrays were 198. That size divides neatly into 3x3 arrays. When you have 200x200 then you would have 2 left over after dividing into groups of 3. What do you want to do with the extra 2x3 and 3x2 and the corner 2x2?
Darsana P M
on 26 Apr 2018
Walter Roberson
on 26 Apr 2018
[r, c] = size(L0);
temp = L0;
temp( r+1:ceil(r/3)*3, c+1:ceil(c/3)*3 ) = 0;
[r, c] = size(temp);
blocks = mat2cell(temp, 3*ones(1,r/3), 3*ones(1,c/3));
clear temp;
Darsana P M
on 26 Apr 2018
Walter Roberson
on 26 Apr 2018
this_block = blocks{7,4}; %extract 3x3 matrix
new_block = reshape(sort(this_block(:)), size(this_block)); %perform certain operations
new_blocks{7,4} = new_block;
new_matrix = cell2mat(new_blocks);
new_matrix = new_matrix(1:size(L0,1), 1:size(L0,2)); %trim out padding
Darsana P M
on 26 Apr 2018
Darsana P M
on 26 Apr 2018
Walter Roberson
on 26 Apr 2018
new_blocks = blocks;
before the above code would ensure that the blocks not changed were copied over.
Your operations would replace
new_block = reshape(sort(this_block(:)), size(this_block)); %perform certain operations
Do whatever operations you need to this_block, assigning the result to new_block .
Darsana P M
on 27 Apr 2018
Walter Roberson
on 27 Apr 2018
>> L0 = imread('cameraman.tif');
>> [r, c] = size(L0);
temp = L0;
temp( r+1:ceil(r/3)*3, c+1:ceil(c/3)*3 ) = 0;
[r, c] = size(temp);
blocks = mat2cell(temp, 3*ones(1,r/3), 3*ones(1,c/3));
clear temp;
>> size(blocks)
ans =
86 86
>> new_blocks = blocks;
>>
this_block = blocks{7,4}; %extract 3x3 matrix
new_block = reshape(sort(this_block(:)), size(this_block)); %perform certain operations
new_blocks{7,4} = new_block;
new_matrix = cell2mat(new_blocks);
new_matrix = new_matrix(1:size(L0,1), 1:size(L0,2)); %trim out padding
>> size(new_matrix)
ans =
256 256
>> size(L0)
ans =
256 256
>> image(new_matrix)
>> colormap(gray(256))
Darsana P M
on 27 Apr 2018
Darsana P M
on 27 Apr 2018
Walter Roberson
on 27 Apr 2018
The result if your dicomread() is likely to be either uint8 or int16, maybe uint16. rgb2gray() will preserve that data type. The way you extract data into L0 looks like it will preserve that data type. The mat2cell() will preserve the data type.
So this_block should be the same data type as dicomread() returned, probably either uint8 or int16. You try to .* with double data. That would be trying to calculate with mixed data types. MATLAB is likely to refuse.
If MATLAB did not refuse, then it looks plausible to me that some of the entries might try to be negative even though the original values were positive. If so then you have a problem, because if the original were uint8 then negatives are not possible and the negatives are going to be converted to 0.
Try
n1 = double(this_block).*Rot;
and when you go to write back into the array,
new_blocks{7,4} = cast(new_block, class(this_block));
which will force the output to be the same data type as the original.
Darsana P M
on 27 Apr 2018
Walter Roberson
on 27 Apr 2018
Difficult to say, as the code you posted does not define new_block.
Darsana P M
on 27 Apr 2018
Categories
Find more on Get Started with Statistics and Machine Learning Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!