Is there any possible way to shorten this code without using dynamic variables

1 view (last 30 days)
I have written this code but it is redundant and repetetive and me coming from other languages I would have used dynamic variables but its not recommended in matlab
and here is the code
x = imread('cameraman.tif');
watermark = imread('C:\Users\areee\Desktop\watermark.png');
x = imresize(x,[512 512]);
watermark = imresize(watermark,[512 512]);
watermark = im2bw(watermark);
I = x;
img_blocks = mat2cell(I,size(I,1)/4*ones(1,4), size(I,2)/4*ones(1,4));
part1 = dct2(img_blocks{1,1});
part2 = dct2(img_blocks{1,2});
part3 = dct2(img_blocks{1,3});
part4 = dct2(img_blocks{1,4});
part5 = dct2(img_blocks{2,1});
part6 = dct2(img_blocks{2,2});
part7 = dct2(img_blocks{2,3});
part8 = dct2(img_blocks{2,4});
part9 = dct2(img_blocks{3,1});
part10 = dct2(img_blocks{3,2});
part11= dct2(img_blocks{3,3});
part12 = dct2(img_blocks{3,4});
part13 = dct2(img_blocks{4,1});
part14 = dct2(img_blocks{4,2});
part15= dct2(img_blocks{4,3});
part16 = dct2(img_blocks{4,4});
I = watermark;
img_blocks = mat2cell(I,size(I,1)/4*ones(1,4), size(I,2)/4*ones(1,4));
partW1 = dct2(img_blocks{1,1});
partW2 = dct2(img_blocks{1,2});
partW3 = dct2(img_blocks{1,3});
partW4 = dct2(img_blocks{1,4});
partW5 = dct2(img_blocks{2,1});
partW6 = dct2(img_blocks{2,2});
partW7 = dct2(img_blocks{2,3});
partW8 = dct2(img_blocks{2,4});
partW9 = dct2(img_blocks{3,1});
partW10 = dct2(img_blocks{3,2});
partW11= dct2(img_blocks{3,3});
partW12 = dct2(img_blocks{3,4});
partW13 = dct2(img_blocks{4,1});
partW14 = dct2(img_blocks{4,2});
partW15= dct2(img_blocks{4,3});
partW16 = dct2(img_blocks{4,4});
newP1 = part1+partW1;
newP2 = part2+partW2;
newP3 = part3+partW3;
newP4 = part4+partW4;
newP5 = part5+partW5;
newP6 = part6+partW6;
newP7 = part7+partW7;
newP8 = part8+partW8;
newP9 = part9+partW9;
newP10 = part10+partW10;
newP11 = part11+partW11;
newP12 = part12+partW12;
newP13 = part13+partW13;
newP14 = part14+partW14;
newP15 = part15+partW15;
newP16 = part16+partW16;
newP1 = idct2(newP1);
newP2 = idct2(newP2);
newP3 = idct2(newP3);
newP4 = idct2(newP4);
newP5 = idct2(newP5);
newP6 = idct2(newP6);
newP7 = idct2(newP7);
newP8 = idct2(newP8);
newP9 = idct2(newP9);
newP10 = idct2(newP10);
newP11 = idct2(newP11);
newP12 = idct2(newP12);
newP13 = idct2(newP13);
newP14 = idct2(newP14);
newP15 = idct2(newP15);
newP16 = idct2(newP16);
total = [newP1,newP2,newP3,newP4;newP5,newP6,newP7,newP8;newP9,newP10,newP11,newP12;newP13,newP14,newP15,newP16];
imshow(total,[]);

Accepted Answer

James Tursa
James Tursa on 29 Apr 2019
Edited: James Tursa on 29 Apr 2019
cellfun might help you. E.g., this
part1 = dct2(img_blocks{1,1});
:
part16 = dct2(img_blocks{4,4});
could be replaced with this:
part = cellfun(@dct2,img_blocks,'uni',false);
Similar for partW. And then another cellfun with @plus for the newP. Etc.
And then for total use cell2mat(newP)

More Answers (0)

Categories

Find more on Linear Algebra 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!