Unable to perform assignment because the left and right sides have a different number of elements.

13 views (last 30 days)
Bersin Tekmen
Bersin Tekmen on 26 Apr 2022
Edited: Jan on 27 Apr 2022
Does anyone know why I get that failure: "Unable to perform assignment because the left and right sides have a different number of elements."
I can't see it. Thanks for your help.
%Files and Pics
data_list = dir('*.bmp');
%Zugreifen auf Ordner und BIlder und Namen zuweisen
name = {data_list.name};
%Konvertieren in Matrix
imBlack = imread("460_dunkel.bmp");
name(strcmp(name,"460_dunkel.bmp")) = []; % remove 460_dunkel.bmp
lambda = 0.633; %Wellenlänge Laser in um
Pixelgroesse = 4.65; %Pixelgroesse in um
for i = 1:22
picture = imread(name{i}); %Einlesen des Bildes in Matrix
current_name = name{i};
zaxe = str2double(name{i}(1:3)); %Auslesen des z Wertes aus Dateinamen
imBlack = double(imBlack);
pictureS(:,:,i)=picture; %speichert das aktuelle (i-te)
%Bild als 2D Matrix in der 3D Matrix (:,:,i)
picture = double(picture);
filtered = double(minus(picture,imBlack));
%Ermittlung ROI
ROI = im2double(filtered);
ROIdiff = [1,1];
ROIoffset =[0, 0];
en_density = sum(sum((ROI)));
%1.Moment
[row,column] = size(ROI);
first_Moment_X(i,1)=0;
for y=1:row
for x=1:column
first_Moment_X(i,1) =first_Moment_X(i,1)+ x.*ROI(y,x);
end
end
Schwerp_X = first_Moment_X./en_density;
Schwerpunkte_X(i) = Schwerp_X;
first_Moment_Y(i,1)=0;
for y=1:row
for x=1:column
first_Moment_Y(i,1) = first_Moment_Y(i,1) + y.*ROI(y,x);
end
end
Schwerp_Y = first_Moment_Y./en_density;
Schwerpunkte_Y(i) = Schwerp_Y;
%2.Moment
[row,column] = size(ROI);
sec_Moment_X(i,1)=0;
for y=1:row
for x=1:column
sec_Moment_X(i,1) = sec_Moment_X(i,1) + ((x-Schwerp_X).^2).*ROI(y,x);
end
end
Sigma_X = sec_Moment_X./en_density;
Sigma_X_plural(i) = Sigma_X;
sec_Moment_Y(i,1) = 0;
for y=1:row
for x=1:column
sec_Moment_Y(i,1) = sec_Moment_Y(i,1) + ((y-Schwerp_Y).^2).*ROI(y,x);
end
end
Sigma_Y = sec_Moment_Y./en_density;
Sigma_Y_plural(i) = Sigma_Y;
sec_Moment_XY(i,1) = 0;
for y=1:row
for x=1:column
sec_Moment_XY(i,1) = sec_Moment_XY(i,1) + (x-Schwerp_X).*(y-Schwerp_Y).*ROI(y,x);
end
end
Sigma_XY = sec_Moment_XY./en_density;
Sigma_XY_plural(i) = Sigma_XY;
%Berechnung des Strahldurchmessers nach Formel 18 und 19 in Norm
dxPixel = 2*sqrt(2)*(sqrt(Sigma_X_plural + Sigma_Y_plural+sign(Sigma_X_plural-Sigma_Y_plural).*(sqrt(((Sigma_X_plural-Sigma_Y_plural).^2)+4*(Sigma_XY_plural.^2)))));
dyPixel = 2*sqrt(2)*(sqrt(Sigma_X_plural + Sigma_Y_plural-sign(Sigma_X_plural-Sigma_Y_plural).*(sqrt(((Sigma_X_plural-Sigma_Y_plural).^2)+4*(Sigma_XY_plural.^2)))));
dx(i,1) = dxPixel * Pixelgroesse; %Umrechnung von Pixel in Mikrometer
dy(i,1) = dyPixel * Pixelgroesse; %Umrechnung von Pixel in Mikrometer
dz(i,1) = zaxe; %Nur um alles am selben Fleck zu haben
end
%Plotten der einzelnen Bilder für dx
scatter(dz,dx,50,'x','MarkerFacecolor','none');
[dz,dx] = prepareCurveData(dz,dx);
hold on;
grid on;
[fitx,gofx]= fit(dz,dx,"poly2");
fithandle = plot(fitx);
set(fithandle,'color','[0 .7 .7]');
[fitxw,gofxw] = fit(dz,dx,'poly2','weights',1./dx);
  5 Comments
Jan
Jan on 27 Apr 2022
@Bersin Tekmen: It is much easier to fix a code, when it is written as simple as possible. These two blocks of code do exactly the same. Compare it for the full program:
This part:
first_Moment_X(i,1)=0;
for y=1:row
for x=1:column
first_Moment_X(i,1) =first_Moment_X(i,1)+ x.*ROI(y,x);
end
end
Schwerp_X = first_Moment_X./en_density;
Schwerpunkte_X(i) = Schwerp_X;
first_Moment_Y(i,1)=0;
for y=1:row
for x=1:column
first_Moment_Y(i,1) = first_Moment_Y(i,1) + y.*ROI(y,x);
end
end
Schwerp_Y = first_Moment_Y./en_density;
Schwerpunkte_Y(i) = Schwerp_Y;
[row,column] = size(ROI);
sec_Moment_X(i,1)=0;
for y=1:row
for x=1:column
sec_Moment_X(i,1) = sec_Moment_X(i,1) + ((x-Schwerp_X).^2).*ROI(y,x);
end
end
Sigma_X = sec_Moment_X./en_density;
Sigma_X_plural(i) = Sigma_X;
sec_Moment_Y(i,1) = 0;
for y=1:row
for x=1:column
sec_Moment_Y(i,1) = sec_Moment_Y(i,1) + ((y-Schwerp_Y).^2).*ROI(y,x);
end
end
Sigma_Y = sec_Moment_Y./en_density;
Sigma_Y_plural(i) = Sigma_Y;
sec_Moment_XY(i,1) = 0;
for y=1:row
for x=1:column
sec_Moment_XY(i,1) = sec_Moment_XY(i,1) + (x-Schwerp_X).*(y-Schwerp_Y).*ROI(y,x);
end
end
Sigma_XY = sec_Moment_XY./en_density;
Sigma_XY_plural(i) = Sigma_XY;
and this one:
xv = (1:column);
yv = (1:row).';
first_Moment_X(i,1) = sum(xv .* ROI, 'all');
Schwerp_X = first_Moment_X ./ en_density;
Schwerpunkte_X(i) = Schwerp_X;
first_Moment_Y(i,1) = sum(yv .* ROI, 'all');
Schwerp_Y = first_Moment_Y ./ en_density;
Schwerpunkte_Y(i) = Schwerp_Y;
sec_Moment_X(i,1) = sum((xv - Schwerp_X).^2 .* ROI, 'all');
Sigma_X_plural(i) = sec_Moment_X./en_density;
sec_Moment_Y(i,1) = sum((yv - Schwerp_Y).^2 .* ROI, 'all');
Sigma_Y_plural(i) = sec_Moment_Y ./ en_density;
sec_Moment_XY(i,1) = sum(xv - Schwerp_X) .* (yv - Schwerp_Y) .* ROI, 'all');
Sigma_XY_plural(i) = sec_Moment_XY ./ en_density;
Are equivalent. I find the 2nd one much easier to read.

Sign in to comment.

Accepted Answer

Jan
Jan on 26 Apr 2022
Schwerp_X = first_Moment_X./en_density;
Schwerpunkte_X(i) = Schwerp_X;
first_Moment_X is a vector, so is Schwerp_X, but Schwerpunkte_X(i) is a scalar.
Maybe you want:
Schwerp_Y = first_Moment_Y(i) / en_density;
Schwerpunkte_Y(i) = Schwerp_Y;
  7 Comments
Jan
Jan on 27 Apr 2022
@Bersin Tekmen: I cannot follow you. After
zaxe = str2double(name{i}(1:3));
zaxe is a scalar. What does it mean that it is "a vector normally"? In your code it is not.
If you want to store the value of zaxe in each iteration, use:
zaxe(i, 1) = str2double(name{i}(1:3));
Then you cann access zaxe(i,1) later on also.

Sign in to comment.

More Answers (1)

Torsten
Torsten on 26 Apr 2022
Only
Schwerpunkte_X(i) = Schwerp_X(i,1);
is possible since "Schwerpunkte_X(i)" is a scalar (one single value) which cannot be set to the vector Schwerp_X.
Same for the following 4 settings
Schwerpunkte_Y(i) = Schwerp_Y;
Sigma_X_plural(i) = Sigma_X;
Sigma_Y_plural(i) = Sigma_Y;
Sigma_XY_plural(i) = Sigma_XY;

Community Treasure Hunt

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

Start Hunting!