Info

This question is closed. Reopen it to edit or answer.

Help me with for-loop

1 view (last 30 days)
Mohammad Alwardat
Mohammad Alwardat on 28 Mar 2020
Closed: MATLAB Answer Bot on 20 Aug 2021
Hi,
I have RGB image and I need to study the similarity between the three layers of my image.
each layer is a matrix.
I need to patch each matrix and study the similarity between matrix 1 and 2 , matrix 1 and 3 , matrix 2 and 3, and record the result of each step in single matrix.
I will attach sample of my code and I need reduce the command in the code using foor loop.
Thanks
I = imread('l4_7.jpg'); I = imresize(I,[256 256]);
A1 = I (:,:,1);
A2 = I (:,:,2);
A3 = I (:,:,3);
imSzA1 = size(A1);
patchSzA1 = [64 64];
xIdxs = [1:patchSzA1(2):imSzA1(2) imSzA1(2)+1];
yIdxs = [1:patchSzA1(1):imSzA1(1) imSzA1(1)+1];
patchesA1 = cell(length(yIdxs)-1,length(xIdxs)-1);
for i = 1:length(yIdxs)-1
Isub = A1(yIdxs(i):yIdxs(i+1)-1,:);
for j = 1:length(xIdxs)-1
patchesA1{i,j} = Isub(:,xIdxs(j):xIdxs(j+1)-1);
end
end
imSzA2 = size(A2);
patchSzA2 = [64 64];
xIdxs = [1:patchSzA2(2):imSzA2(2) imSzA2(2)+1];
yIdxs = [1:patchSzA2(1):imSzA2(1) imSzA2(1)+1];
patchesA2 = cell(length(yIdxs)-1,length(xIdxs)-1);
for i = 1:length(yIdxs)-1
Isub = A2(yIdxs(i):yIdxs(i+1)-1,:);
for j = 1:length(xIdxs)-1
patchesA2{i,j} = Isub(:,xIdxs(j):xIdxs(j+1)-1);
end
end
A11 = double(patchesA1{1,1});
A12 = double(patchesA1{1,2});
A13 = double(patchesA1{1,3});
A14 = double(patchesA1{1,4});
A21 = double(patchesA1{2,1});
A22 = double(patchesA1{2,2});
A23 = double(patchesA1{2,3});
A24 = double(patchesA1{2,4});
A31 = double(patchesA1{3,1});
A32 = double(patchesA1{3,2});
A33 = double(patchesA1{3,3});
A34 = double(patchesA1{3,4});
A41 = double(patchesA1{4,1});
A42 = double(patchesA1{4,2});
A43 = double(patchesA1{4,3});
A44 = double(patchesA1{4,4});
%----------------------
B11 = double(patchesA2{1,1});
B12 = double(patchesA2{1,2});
B13 = double(patchesA2{1,3});
B14 = double(patchesA2{1,4});
B21 = double(patchesA2{2,1});
B22 = double(patchesA2{2,2});
B23 = double(patchesA2{2,3});
B24 = double(patchesA2{2,4});
B31 = double(patchesA2{3,1});
B32 = double(patchesA2{3,2});
B33 = double(patchesA2{3,3});
B34 = double(patchesA2{3,4});
B41 = double(patchesA2{4,1});
B42 = double(patchesA2{4,2});
B43 = double(patchesA2{4,3});
B44 = double(patchesA2{4,4});
%-----------
A = zeros(16,16);
A11 = reshape(A11,4096,1);
A12 = reshape(A12,4096,1);
A13 = reshape(A13,4096,1);
A14 = reshape(A14,4096,1);
A51 = reshape(A21,4096,1);
A61 = reshape(A22,4096,1);
A23 = reshape(A23,4096,1);
A24 = reshape(A24,4096,1);
A31 = reshape(A31,4096,1);
A32 = reshape(A32,4096,1);
A33 = reshape(A33,4096,1);
A34 = reshape(A34,4096,1);
A41 = reshape(A41,4096,1);
A42 = reshape(A42,4096,1);
A43 = reshape(A43,4096,1);
A44 = reshape(A44,4096,1);
B11 = reshape(B11,4096,1);
B12 = reshape(B12,4096,1);
B13 = reshape(B13,4096,1);
B14 = reshape(B14,4096,1);
B21 = reshape(B21,4096,1);
B22 = reshape(B22,4096,1);
B23 = reshape(B23,4096,1);
B24 = reshape(B24,4096,1);
B31 = reshape(B31,4096,1);
B32 = reshape(B32,4096,1);
B33 = reshape(B33,4096,1);
B34 = reshape(B34,4096,1);
B41 = reshape(B41,4096,1);
B42 = reshape(B42,4096,1);
B43 = reshape(B43,4096,1);
B44 = reshape(B44,4096,1);
%--------------
x = reshape(A11,4096,1);
y = reshape(B11,4096,1);
A(1,1) = getEcludianSimilarity(x,y);
x = reshape(A11,4096,1);
y = reshape(B12,4096,1);
A(1,2) = getEcludianSimilarity(x,y);
x = reshape(A11,4096,1);
y = reshape(B13,4096,1);
A(1,3) = getEcludianSimilarity(x,y);
x = reshape(A11,4096,1);
y = reshape(B14,4096,1);
A(1,4) = getEcludianSimilarity(x,y);
x = reshape(A11,4096,1);
y = reshape(B21,4096,1);
A(1,5) = getEcludianSimilarity(x,y);
x = reshape(A11,4096,1);
y = reshape(B22,4096,1);
A(1,6) = getEcludianSimilarity(x,y);
x = reshape(A11,4096,1);
y = reshape(B23,4096,1);
A(1,7) = getEcludianSimilarity(x,y);
x = reshape(A11,4096,1);
y = reshape(B24,4096,1);
A(1,8) = getEcludianSimilarity(x,y);
x = reshape(A11,4096,1);
y = reshape(B31,4096,1);
A(1,9) = getEcludianSimilarity(x,y);
x = reshape(A11,4096,1);
y = reshape(B32,4096,1);
A(1,10) = getEcludianSimilarity(x,y);
x = reshape(A11,4096,1);
y = reshape(B33,4096,1);
A(1,11) = getEcludianSimilarity(x,y);
x = reshape(A11,4096,1);
y = reshape(B34,4096,1);
A(1,12) = getEcludianSimilarity(x,y);
x = reshape(A11,4096,1);
y = reshape(B41,4096,1);
A(1,13) = getEcludianSimilarity(x,y);
x = reshape(A11,4096,1);
y = reshape(B42,4096,1);
A(1,14) = getEcludianSimilarity(x,y);
x = reshape(A11,4096,1);
y = reshape(B43,4096,1);
A(1,15) = getEcludianSimilarity(x,y);
x = reshape(A11,4096,1);
y = reshape(B44,4096,1);
A(1,16) = getEcludianSimilarity(x,y);
  4 Comments
Walter Roberson
Walter Roberson on 28 Mar 2020
A11 = double(patchesA1{1,1});
A12 = double(patchesA1{1,2});
A13 = double(patchesA1{1,3});
A14 = double(patchesA1{1,4});
A21 = double(patchesA1{2,1});
A22 = double(patchesA1{2,2});
A23 = double(patchesA1{2,3});
A24 = double(patchesA1{2,4});
A31 = double(patchesA1{3,1});
A32 = double(patchesA1{3,2});
A33 = double(patchesA1{3,3});
A34 = double(patchesA1{3,4});
A41 = double(patchesA1{4,1});
A42 = double(patchesA1{4,2});
A43 = double(patchesA1{4,3});
A44 = double(patchesA1{4,4});
Gets replaced with
A = cellfun(@double, patchesA1);
getting out a cell array instead of getting out a number of variables.
but instead of doing that, why not
patchesA1{i,j} = double(Isub(:,xIdxs(j):xIdxs(j+1)-1));
in which case the code
A11 = double(patchesA1{1,1});
A12 = double(patchesA1{1,2});
A13 = double(patchesA1{1,3});
A14 = double(patchesA1{1,4});
A21 = double(patchesA1{2,1});
A22 = double(patchesA1{2,2});
A23 = double(patchesA1{2,3});
A24 = double(patchesA1{2,4});
A31 = double(patchesA1{3,1});
A32 = double(patchesA1{3,2});
A33 = double(patchesA1{3,3});
A34 = double(patchesA1{3,4});
A41 = double(patchesA1{4,1});
A42 = double(patchesA1{4,2});
A43 = double(patchesA1{4,3});
A44 = double(patchesA1{4,4});
would become
A = patchesA1;
Mohammad Alwardat
Mohammad Alwardat on 28 Mar 2020
I need to reduce this commands
x = reshape(A11,4096,1);
y = reshape(B11,4096,1);
A(1,1) = getEcludianSimilarity(x,y);
x = reshape(A11,4096,1);
y = reshape(B12,4096,1);
A(1,2) = getEcludianSimilarity(x,y);
x = reshape(A11,4096,1);
y = reshape(B13,4096,1);
A(1,3) = getEcludianSimilarity(x,y);
x = reshape(A11,4096,1);
y = reshape(B14,4096,1);
A(1,4) = getEcludianSimilarity(x,y);
x = reshape(A11,4096,1);
y = reshape(B21,4096,1);
A(1,5) = getEcludianSimilarity(x,y);
x = reshape(A11,4096,1);
y = reshape(B22,4096,1);
A(1,6) = getEcludianSimilarity(x,y);
x = reshape(A11,4096,1);
y = reshape(B23,4096,1);
A(1,7) = getEcludianSimilarity(x,y);
x = reshape(A11,4096,1);
y = reshape(B24,4096,1);
A(1,8) = getEcludianSimilarity(x,y);
x = reshape(A11,4096,1);
y = reshape(B31,4096,1);
A(1,9) = getEcludianSimilarity(x,y);
x = reshape(A11,4096,1);
y = reshape(B32,4096,1);
A(1,10) = getEcludianSimilarity(x,y);
x = reshape(A11,4096,1);
y = reshape(B33,4096,1);
A(1,11) = getEcludianSimilarity(x,y);
x = reshape(A11,4096,1);
y = reshape(B34,4096,1);
A(1,12) = getEcludianSimilarity(x,y);
x = reshape(A11,4096,1);
y = reshape(B41,4096,1);
A(1,13) = getEcludianSimilarity(x,y);
x = reshape(A11,4096,1);
y = reshape(B42,4096,1);
A(1,14) = getEcludianSimilarity(x,y);
x = reshape(A11,4096,1);
y = reshape(B43,4096,1);
A(1,15) = getEcludianSimilarity(x,y);
x = reshape(A11,4096,1);
y = reshape(B44,4096,1);
A(1,16) = getEcludianSimilarity(x,y);

Answers (0)

This question is closed.

Tags

Community Treasure Hunt

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

Start Hunting!