Error using + Matrix dimensions must agree.

this error appears to me every time!! please help!!
this is my code :
clear;
clf;
% DM = Data matrix (d x n)
% columns are data points
s=0.25;
S1= [1 2]' + s*randn(2,100);
S2= [2 3]' + s*randn(2,100);
S3= [3 2]' + s*randn(2,100);
DM= [S1 S2 S3];
DM=DM(:,randperm(size(DM,2)));
plot(DM(1,:),DM(2,:),'yo'); hold on;
% M = Membership row vector (n)
% entries are membership values
% C = Cluster centers vector (d x k)
% each column is a cluster center
% Select k
k = 2;
n = size(DM,2);
d = size(DM,1);
% Forgy initialization: Choose random k points as cluster centers
pVec=randperm(n);
C=DM(:,pVec(1:k));
plot(C(1,:),C(2,:),'rx');
input('Press ENTER');
while (1)
err=0;
% Update M
for i=1:n
VL = C - DM(:,i);
[y,j]= min(diag(VL'*VL));
M(i)= j;
err= err + y;
end
disp(err);
% Update C
Cp=C;
for i=1:k
C(:,i)=mean(DM(:,M == i),2);
end
for i=1:k
plot([Cp(1,i) C(1,i)],[Cp(2,i) C(2,i)],'r-');
end
plot(C(1,:),C(2,:),'rx');
input('Press ENTER');
end

Antworten (3)

Walter Roberson
Walter Roberson am 28 Nov. 2017

1 Stimme

"my intention is o add 1 to every element in the first row of the 2x100 array, and add 2 to every element in the second row"
S1 = bsxfun(@plus, [1 2].', s*randn(2,100));
The code you had, S1= [1 2]' + s*randn(2,100); is valid in R2016b or later but not in earlier MATLAB.
Geoff Hayes
Geoff Hayes am 28 Nov. 2017

0 Stimmen

Ibrahim - the error is with this line (and the ones that follow)
S1= [1 2]' + s*randn(2,100);
s is a scalar and so
s*randn(2,100)
is a 2x100 array of random numbers multiplied by that scalar. You are then trying to add a 2x1 array to the 2x100 array. What is your intention? To concatenate the two arrays so that you get a 2x101 array like
S1 = [ [1 2]' s*randn(2,100)];
or do you want to add 1 to every element in the first row of the 2x100 array, and add 2 to every element in the second row?

4 Kommentare

Ibrahim Alkhalifah
Ibrahim Alkhalifah am 28 Nov. 2017
my intention is o add 1 to every element in the first row of the 2x100 array, and add 2 to every element in the second row
S1 = randn(2,100);
S1 = [s*S1(1,:) + 1 ; s*S1(2,:) + 2];
Same for S2 and S3.
Best wishes
Torsten.
this error appeared!! :
Attempted to access S2(3,:); index out of bounds because size(S2)=[2,100].
S1 = randn(2,100);
S1 = [s*S1(1,:) + 1 ; s*S1(2,:) + 2];
S2 = randn(2,100);
S2 = [s*S2(1,:) + 2 ; s*S2(2,:) + 3];
S3 = randn(2,100);
S3 = [s*S3(1,:) + 3 ; s*S3(2,:) + 2];
Best wishes
Torsten.

Melden Sie sich an, um zu kommentieren.

mohamed mohsen
mohamed mohsen am 30 Mai 2018
Bearbeitet: Walter Roberson am 30 Mai 2018

0 Stimmen

function [motionVect, EScomputations] = motionEstES(imgP, imgI, mbSize, p)
[row ,col] = size(imgI);
vectors = zeros(2,row*col/mbSize^2);
costs = ones(2*p + 1, 2*p +1) * 65537;

4 Kommentare

mohamed mohsen
mohamed mohsen am 30 Mai 2018
Error in motionEstES (line 17) [row ,col] = size(imgI);
Walter Roberson
Walter Roberson am 30 Mai 2018
Bearbeitet: Walter Roberson am 30 Mai 2018
You would get that error if you attempted to run that code by clicking on the green Run button instead of calling the code passing in appropriate parameters. It appears that the code expects that the second parameter will be a grayscale image (not an RGB image, and not the filename of an image.)
mohamed mohsen
mohamed mohsen am 30 Mai 2018
sorry, sir, I didn't get it? what exactly I suppose to write in the code?
thanx for your time
im1 = imread('FirstFile.png');
im2 = imread('SecondFile.png');
gim1 = rgb2gray(im1);
gim2 = rgb2gray(im2);
mbSize = 16;
p = 5;
[motionvect, escomp] = motionEstES(gim1, gim2, mbSize, p);

Melden Sie sich an, um zu kommentieren.

Tags

Gefragt:

am 28 Nov. 2017

Kommentiert:

am 30 Mai 2018

Community Treasure Hunt

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

Start Hunting!

Translated by