フィルターのクリア

Error using + Matrix dimensions must agree.

1 回表示 (過去 30 日間)
Ibrahim Alkhalifah
Ibrahim Alkhalifah 2017 年 11 月 28 日
コメント済み: Walter Roberson 2018 年 5 月 30 日
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

回答 (3 件)

Walter Roberson
Walter Roberson 2017 年 11 月 28 日
"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 2017 年 11 月 28 日
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 件のコメント
Ibrahim Alkhalifah
Ibrahim Alkhalifah 2017 年 11 月 28 日
this error appeared!! :
Attempted to access S2(3,:); index out of bounds because size(S2)=[2,100].
Torsten
Torsten 2017 年 11 月 28 日
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.

サインインしてコメントする。


mohamed mohsen
mohamed mohsen 2018 年 5 月 30 日
編集済み: Walter Roberson 2018 年 5 月 30 日
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 件のコメント
mohamed mohsen
mohamed mohsen 2018 年 5 月 30 日
sorry, sir, I didn't get it? what exactly I suppose to write in the code?
thanx for your time
Walter Roberson
Walter Roberson 2018 年 5 月 30 日
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);

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeParticle & Nuclear Physics についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by