hello folks, I'm trying to run 2 different dimensional vectors by using weighted regions with a chosen factor. can I get some expert opinion on identify the error,please. your help is gratefully appreciated.Many thanks in advance.
x=train_data; % 882 x2 double
z=test_data; %882 x 8 double
distance_max=[];
for h=1:size(x)
for kb=1:size(z)
kk=x(:,h);
gg=z(:,kb); <-- this is where the error appeared
weightsum=0;
ww = [0, 1,1,1,1,1,0,0,1,1,1,1,1,0,1,1,2,1,2,1,1,2,4,4,0,4,4,2,1,1,1,1,1,1,1,0,1,2,2,2,1,0,0,0,1,1,1,0,0];
length=18;
region = size(x,1)/length;
for e=1:region
e_start = (e -1) * length+1 ;
e_end = e_start + length-1 ;
xpart=kk(e_start:e_end);
zpart = gg (e_start:e_end);
D1 = pdist2(xpart',zpart','chisq')*ww(e);
distance_max=[distance_max,D1];
weightsum=weightsum+D1;
end
results(h,k)={weightsum};
end
end

5 件のコメント

Adam
Adam 2014 年 9 月 11 日
編集済み: Adam 2014 年 9 月 11 日
Presumably you got a line number with that error that tells you the line which causes the error?
It is a lot more difficult for us to look through a whole block of code hunting for an error when the error message should just point straight to it!
What is y by the way?
seprienna
seprienna 2014 年 9 月 11 日
編集済み: seprienna 2014 年 9 月 11 日
this is the line
Index exceeds matrix dimensions.
Error in weight (line 13)
gg=z(:,kb);
I have edited it in the code.
There is no 'y', its only 'x'-training data and 'z'-testing data
Adam
Adam 2014 年 9 月 11 日
編集済み: Adam 2014 年 9 月 11 日
There is a
for kb=1:size(y)
line in your code though so when I tried to run it with random matrices of the correct size in place of your x and z that threw an error.
Should that just be size(z) then instead of size(y)?
seprienna
seprienna 2014 年 9 月 11 日
編集済み: seprienna 2014 年 9 月 11 日
million apologies for the confusion adam. I was using the y for a different data but in this experiment, i'm using 'x' and 'z'. Can you kindly point out how can i avoid the dimension error please.
Rick Rosson
Rick Rosson 2014 年 9 月 11 日
Two lines above the line that caused the error:
for kb=1:size(y)

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

 採用された回答

Rick Rosson
Rick Rosson 2014 年 9 月 11 日
編集済み: Rick Rosson 2014 年 9 月 11 日

0 投票

Please try changing the line
for kb=1:size(y)
to the following:
for kb=1:size(z)
Also, please correct the issue with size described in the answer provided by Adam.

3 件のコメント

seprienna
seprienna 2014 年 9 月 11 日
Hello Rick,
I'm aware with the size problem. I have followed your advise, but it looks like I'm not getting what i wanted. If you could look above for my comment to Adam, maybe you can enlighten my issue. your opinion is greatly appreciated.
Rick Rosson
Rick Rosson 2014 年 9 月 11 日
I think you want to loop over the number of columns in each matrix. So please try the following:
for h=1:size(x,2)
for kb=1:size(z,2)
seprienna
seprienna 2014 年 9 月 11 日
Rick,thanks for the tips. that is the result i'm looking for. thanks again.

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

その他の回答 (1 件)

Adam
Adam 2014 年 9 月 11 日
編集済み: Adam 2014 年 9 月 11 日

2 投票

size(x)
will return a length d vector where d is the dimension of x. In your case that will be [882 2] and likewise size(y) will return [882 8].
If you want the first dimension you can use
size( x, 1 )
to give 882, or
size( x, 2 )
if you want the second dimension only. You can use
length(x)
if you just want the longest dimension but don't know what the index of that dimension is, but I don't recommend using length for this purpose if you do know which dimension you want.
That may not be the only problem, but it will definitely cause problems.

3 件のコメント

seprienna
seprienna 2014 年 9 月 11 日
編集済み: seprienna 2014 年 9 月 11 日
hello Adam, many thanks for the helpful information.
i need both dimensions as the are 2 images in the (x) and (z) are the 8 testing images.
i need to calculate the chi-square distance between (x) and (z).
Initially I used size(x,1) to calculate 1 image only, but with 2 images, i wish to do it in a loop, I read from the MATLAB guide that it is possible, but I'm not sure where I made the mistake.
Pierre Benoit
Pierre Benoit 2014 年 9 月 11 日
Well from what your code do, it seems that you need to use
size(x,2)
and the same for z.
seprienna
seprienna 2014 年 9 月 11 日
it works!!!thanks you very much

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

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品

質問済み:

2014 年 9 月 11 日

コメント済み:

2014 年 9 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by