Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

Dimension must agree.Where am I doing wrong?

1 visualización (últimos 30 días)
pranith kumar
pranith kumar el 2 de Mayo de 2016
Cerrada: MATLAB Answer Bot el 20 de Ag. de 2021
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 comentarios
Stephen23
Stephen23 el 2 de Mayo de 2016
Are you sure that n_centroid and o_centroid have the same number of columns/pages/... ?
pranith kumar
pranith kumar el 2 de Mayo de 2016
Editada: pranith kumar el 2 de Mayo de 2016
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

Respuestas (2)

Image Analyst
Image Analyst el 2 de Mayo de 2016
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 comentarios
pranith kumar
pranith kumar el 2 de Mayo de 2016
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.
Image Analyst
Image Analyst el 2 de Mayo de 2016
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.

louis rassinfosse
louis rassinfosse el 2 de Mayo de 2016
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 comentarios
louis rassinfosse
louis rassinfosse el 2 de Mayo de 2016
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
pranith kumar
pranith kumar el 2 de Mayo de 2016
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.

La pregunta está cerrada.

Community Treasure Hunt

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

Start Hunting!

Translated by