MATLAB Answers

Changing the range of for loop give different result

11 views (last 30 days)
Muhammad Jabir Khan
Muhammad Jabir Khan on 15 Sep 2021
Commented: Prachi Kulkarni on 21 Oct 2021 at 10:44
Hello everyone,
My problem here is with the for loop. If I put the limits of the for loop up to (i=2:100 and j=2:100) it gives us correct result. But if put the same loop for (i=2:r+1and j=2:k+1), then result is [1 1 1; 1 1 1; 1 1 1] for all values. I am dealing with an image, and below is my code.
%% Input Image
clear all;
III = rgb2gray(imread('rice.tif'));%name of the image
I = double(III);
[r,k] = size(I);%no of row and column is I
%% Initization of algo
xmax = max(max(max(I)));%maximum pixel/element of the image; (why apply three max)
%converting into the fuzzy domain from the original image;
fim = I/xmax;%fim is the image data of the input image in the fuzzy domain,all value of the fim in the interval of [0 1];
%initializing the edge image as zeros matrix i.e black box;
fedgeim = zeros(r,k);%in fuzzy domain
%Increaing the boreder line of the iamge i.e to increase the row and column
%by 2 in the first and last by taking the mirror image of the immediate
%existing rows and columns respectively;
r1 = fim(2,:);%Copy of all element in the 2nd row of fim
r2 = fim(r-1,:);
c1 = fim(:,2);c2 = fim(:,k-1);
b1 = [0 r1 0];b2 = [0 r2 0];
b3 = [c1 fim c2];
bfim = [b1;b3;b2];%bfim = Border fuzzy image matix
bfim(1,1) = fim(1,1);
bfim(r+2, k+2) = fim(r,k);
bfim(1,k+2) = fim(1,k);
bfim(r+2,1) = fim(r,1);
for i = 2:r+1
for j = 2:k+1
A = [bfim(i-1,j-1) bfim(i,j-1) bfim(i+1,j-1) ; bfim(i-1,j) bfim(i,j) bfim(i+1,j) ; bfim(i-1,j+1) bfim(i,j+1) bfim(i+1,j+1)]
Prachi Kulkarni
Prachi Kulkarni on 21 Oct 2021 at 10:44
I used the code snippet you have provided on similar rice images and there does not seem to be any problem. I am not getting the [1 1 1;1 1 1;1 1 1] matrix.
Please attach the exact image you are using.

Sign in to comment.

Answers (1)

Jan on 18 Sep 2021
You cannot create a matrix, whose elements are matrices. This works with a cell array:
A = cell(r+1, k+1);
for i = 2:r+1
for j = 2:k+1
A{i, j} = bfim(i-1:i-1, j-1:j+1);
Or with a multipdimensional array:
A = nan(3, 3, r+1, k+1);
for i = 2:r+1
for j = 2:k+1
A(:, :, i, j) = bfim(i-1:i-1, j-1:j+1);

Community Treasure Hunt

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

Start Hunting!