Unable to perform assignment because the size of the left side is 17-by-17 and the size of the right side is 16-by-17.

1 次查看(过去 30 天)
I'm having a problem in storing 17x17 2d array into a 17x17xno.ofmatrix 3d array. its giving me this error
"Unable to perform assignment because the size of the left side is 17-by-17 and the size of the right side is 16-by-17."
but when i remove the code that store the 2d array to the 3d array all 2d array have 17x17 dimension(ichecked using size(temp)). it seems that adding this line of code
array(:,:,ctr)=temp;
caused the temp matrix to error.
here is my code:
clear;clc;
files = dir('C:\Program Files\MATLAB\R2018a\bin\vision\data\');
files(ismember( {files.name}, {'.', '..'})) = [];
dirFlags = [files.isdir];
% Extract only those that are directories.
subFolders = files(dirFlags);
% Print folder names to command window.
trainctr =1;
testctr =1;
trainingLabels = cell(315,1);
testLabels = cell(336,1);
%subfolder loop
for k = 1 : length(subFolders)
%FOR DEBUGGING: Print all subfolders
%fprintf('Sub folder #%d = %s\n', k, subFolders(k).nametest(:,:,1,1));
direc = 'C:\Program Files\MATLAB\R2018a\bin\vision\data\';
newDir = strcat(direc,subFolders(k).name);
subname = subFolders(k).name;
%FOR DEBUGGING: Print new Directory
%fprintf('New Dir: %s\n', newDir);
scanDir = dir(newDir);
scanDir(ismember( {scanDir.name}, {'.', '..'})) = [];
scanFlags = [scanDir.isdir];
scanSubFolders = scanDir(scanFlags);
%note category loop
for i = 1 : length(scanSubFolders)
newDir2 = strcat(newDir,'\');
newDir2 = strcat(newDir2,scanSubFolders(i).name);
newDir3 = newDir2;
newDir2 = strcat(newDir2,'\*.png');
imagefiles = dir(newDir2);
nfiles = length(imagefiles);
fold = scanSubFolders(i).name;
%image loop
for ii=1:nfiles
%fprintf('%s\n',imagefiles(ii).name);
%
photo=imagefiles(ii).name;
filename = fullfile(newDir3,photo);
Image = imread(filename);
%pre processing
A = rgb2gray(Image);
A=imbinarize(A,'adaptive','ForegroundPolarity','dark','sensitivity',0.5);
folder = strcmp('testing',subname);
if folder == 1
test(:,:,testctr) = A;
testLabels(testctr) = {scanSubFolders(i).name};
testctr = testctr+1;
else
train(:,:,trainctr) = A;
trainingLabels(trainctr) = {scanSubFolders(i).name};
trainctr = trainctr+1;
end
end
end
end
%CNN networl
train1=double(train);
padded = ones(36);
for ctr=1:length(train1)
C=train1(:,:,ctr);
tmp=sum(C,2);
ind=find(tmp<10);
C(ind,:)=1;
openedImage = imopen(C, [1;1;1]);
[rows, columns] = size(openedImage);
mask = repmat(tmp<200, 1, columns);
C(mask) = openedImage(mask);
%padding
for i=1:size(C,1)
for j=1:size(C,2)
padded(i+2,j+2)=C(i,j);
end
end
for i=1:size(padded,1)-2
for j=1:size(padded,2)-2
%Sobel mask for x-direction:
Gx=( ( 2*padded(i+2,j+1)+padded(i+2,j)+padded(i+2,j+2) ) - ( 2*padded(i,j+1)+padded(i,j)+padded(i,j+2) ) );
%Sobel mask for y-direction:
Gy=((2*padded(i+1,j+2)+padded(i,j+2)+padded(i+2,j+2))-(2*padded(i+1,j)+padded(i,j)+padded(i+2,j)));
%The gradient of the image
B(i,j)=sqrt(Gx.^2+Gy.^2);
end
end
c=double(B);
q = 0;
x=0;
temp = 0;
for i=1:+2:size(c,1)
for j=1:+2:size(c,2)
var1 = B(i,j);
var2 = B(i,j+1);
var3 = B(i+1,j);
var4 = B(i+1,j+1);
ans = [var1,var2;var3,var4];
if(i == 1)
if(x==0)
q(1) = max(ans(:));
x=x+1;
else
q = [q,max(ans(:))];
end
else
q = [q,max(ans(:))];
end
end
if(i~=1)
q = q(2:end);
end
%length(q)
if(temp == 0)
temp = q;
else
temp = [temp;q];
end
q=0;
%x=x+1;
end
array(:,:,ctr)=temp;
end

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Scripts 的更多信息

标签

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by