You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How to solve the problem?
2 views (last 30 days)
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
blocks = mat2cell(L0, 3*ones(1,22), 3*ones(1,22));
I got an error.here,L0=200x200 matrix?
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
sir, let it be zeros.So how should I modify the code?
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
Sir,the code works fine. From this how to extract 3x3 matrix and then perform certain operations, and then put them into an new matrix,200x200?
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
Index exceeds matrix dimensions.
Error in daru (line 49) new_matrix = new_matrix(1:size(L0,1), 1:size(L0,2)); %trim out padding Sir I got this error?
Darsana P M
on 26 Apr 2018
Sir, also where should I apply my operations?
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
sir,the dimensions became wrong.
Index exceeds matrix dimensions.
Error in daru (line 60) new_matrix = new_matrix(1:size(L0,1), 1:size(L0,2)); %trim out padding
Sir, i have a doubt, after doing all these operations, will a get a 3x3 matrix or the entire image, 200x200??
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
clc;
close all;
clear all;
k=0;
I = dicomread('C:\Users\Click Me\Desktop\NIELIT\Output images\dic5.dcm');
info = dicominfo('C:\Users\Click Me\Desktop\NIELIT\Output images\dic5.dcm');
figure(1);
image(I);
h=rgb2gray(I);
imhist(h);
N= 257;
l=zeros(200,400);
B_mat=h(1:200,:);
figure(2);
image(B_mat);
%Rot=[-15,-20,60,20;52,-39,0,25;36,48,32,13;12,43,23,76];
Rot=[-15,-20,60;52,-39,0;36,48,32]
o=[3 4 2 6];
q1=o;
q2=qInv(q1);
L0=B_mat(:,1:200);
R0=B_mat(:,201:400);
h=qGetR(R0);
[M,N]=size(L0);
b=3;
M=M-rem(M,b);
N=N-rem(N,b);
L0=L0;
%A=zeros(200,200);
lo=L0(1:200,1:200);
%s=size(lo);
% dim=198;
% [row column]=size(lo);
% count=0;
% Bigblock=zeros(198,198);
[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));
%J=cell2mat(blocks);
clear temp;
this_block = blocks{7,4};
%extract 3x3 matrix
n1=(this_block).*Rot;
L1=mod(n1,N);
%B=cat(dim,Bigblock,block);
R1=(L1 + this_block);
m=mod(R1,N);
%new_block=this_block;
%new_block=mat2cell(blocks);
%new_block = reshape(sort(this_block(:)), size(this_block)); %perform certain operations
new_blocks{7,4} = new_block;
new_matrix = cell2mat(new_blocks);
%new_blocks= cell2mat(blocks);
new_matrix = new_matrix(1:size(L0,1), 1:size(L0,2)); %trim out padding
Sir, Thanks a lot for your help.But could you please help me, for me there is an issue doing .* multiplication step.
Darsana P M
on 27 Apr 2018
>> size(L0)
ans =
200 200
>> size(R0)
ans =
200 200
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
Thank you sir
Error using cast Conversion to double from cell is not possible.
Error in daru (line 65) new_blocks{7,4} = cast(new_block,class(this_block));
Sir, I got this error.
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
ok sir, I will check it out. Thanks a lot for your help sir.
See Also
Categories
Find more on Number Theory in Help Center and File Exchange
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)