Dimension must agree.Where am I doing wrong?

o_centroid=zeros(20,2);
%all below runs in a loop where fg1 is binary image which changes after every loop.
tracking=regionprops(fg1,'basic');
n_centroid=cat(1,tracking.Centroid);
if size(n_centroid,1)< size(o_centroid,1) %here trying to get the dimensions of both v1 and v2 to be equal.
v1=o_centroid(1:size(n_centroid,1),:);
v2=n_centroid;
else
v1=n_centroid(1:size(o_centroid,1),:);
v2=o_centroid;
end
v= v1-v2;
v= (((sum(v.^2,2)).^1/2)/0.0417);
o_centroid=n_centroid;
error: error using -
matrix diemnsion must agree.
Error in line
v=v1-v2;

2 Comments

Are you sure that n_centroid and o_centroid have the same number of columns/pages/... ?
yes they are I guess.Even that's there in the code. I declared o_centroid=zeros(20,2);(outside loop) and also all I am again changing is o_centroid=n_centroid which will make their dimensions same again. None of these variables are again used in my code at some other place.
A new update. all these i am running on a video frame by frame. If i skip the frame loop to start from number 5 instead of number 1 this error is gone but some other error arises.Whats is going on.
thanks in advance

Answers (2)

The problem could be caused by you having different number of blobs in the binary images, like 1 some images, and 3 blobs in a different image. After regionprops(), do this:
message = sprintf('This image has %d blobs in it', length(tracking));
uiwait(helpdlg(message));
Does it say that all images have the same number of blobs in it by the time it throws the error?

4 Comments

Thank you sir/madam, You are a saviour.that solved half of my problem.first 5 frames has zero blobs. so I guess o_centroid is going to zero rows before it reach frame 2.
that is solved if i change the loop to run from from 5.now a new error came up. please help Subscript indices must either be real positive integers or logicals.
v= ((sum(v.^2,2)).^1/2)*0.72; (changed this from question given above)
Most likely you have destroyed the built-in sum() function by declaring/creating a variable called "sum". Look over your code for every place where you have the word "sum". If you have a variable with that name, call it "theSum" instead of "sum".
No. The 'sum' u see in the code is not a variable I created. The v matrix is a two column vector. I want each element to be squared and then all the elements in the rows to be added together. v.^2 is squaring each element and sum(a,2) will add all the elements in row to a single column matrix.
But I don't see any other way that
v= ((sum(v.^2,2)).^1/2)*0.72;
would give that error. Nothing else, other than v.^2 could possibly be considered as an index of anything. So it must be sum. What does this say if you put it right before that line of code:
which -all sum
It should show this:
>> which -all sum
built-in (C:\Program Files\MATLAB\R2016a\toolbox\matlab\datafun\@double\sum) % double method
built-in (C:\Program Files\MATLAB\R2016a\toolbox\matlab\datafun\@uint8\sum) % uint8 method
built-in (C:\Program Files\MATLAB\R2016a\toolbox\matlab\datafun\@uint16\sum) % uint16 method
built-in (C:\Program Files\MATLAB\R2016a\toolbox\matlab\datafun\@uint32\sum) % uint32 method
built-in (C:\Program Files\MATLAB\R2016a\toolbox\matlab\datafun\@uint64\sum) % uint64 method
built-in (C:\Program Files\MATLAB\R2016a\toolbox\matlab\datafun\@int8\sum) % int8 method
built-in (C:\Program Files\MATLAB\R2016a\toolbox\matlab\datafun\@int16\sum) % int16 method
built-in (C:\Program Files\MATLAB\R2016a\toolbox\matlab\datafun\@int32\sum) % int32 method
built-in (C:\Program Files\MATLAB\R2016a\toolbox\matlab\datafun\@int64\sum) % int64 method
built-in (C:\Program Files\MATLAB\R2016a\toolbox\matlab\datafun\@single\sum) % single method
built-in (C:\Program Files\MATLAB\R2016a\toolbox\matlab\datafun\@char\sum) % char method
built-in (C:\Program Files\MATLAB\R2016a\toolbox\matlab\datafun\@logical\sum) % logical method
C:\Program Files\MATLAB\R2016a\toolbox\matlab\timefun\@duration\sum.m % duration method
C:\Program Files\MATLAB\R2016a\toolbox\matlab\timeseries\@timeseries\sum.m % timeseries method
If it doesn't then "sum" is the problem. If it shows that then you've probably given the wrong line of code that the error was for and you'll have to give us ALL THE RED TEXT so we can discover the true line of code that is throwing the error. Or else just give us the whole m-file and dependent m-files.
v1=o_centroid(1:size(n_centroid,1) ,:);
v2=n_centroid;
i think your mistake is here, try: v1 = o_centroid(1:size(n_centroid,1));

3 Comments

my n_centroid matrix is a 2 column matrix. making it
v1 = o_centroid(1:size(n_centroid,1));
will return only first column matrix . But i need both columns
what about your o centroid ? 2 columns as well ? because you cannot sum a 1 column with a 2 column matrix. However, if both have 2 columns, try to find your error by opening v1 and v2 separatly and looking at how they have been changed compared to n and o centroid
Yes. o_centroid is a 2 column matrix. before beginning the loop i declared o_centroid=zeros(20,2) and at the end of loop(before ending loop) all iam altering o_centroid is o_centorid=n_centroid; I dont understand where am I playing with the dimensions of any of those.

This question is closed.

Asked:

on 2 May 2016

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!