What's wrong with my for loop?....error is that matric dimensions must match.

Bii=B1{ii};
for ii= 1:N
Bii=B1{ii};
[x,y]=find(B1{ii}==max(max(B1{ii})));
[X,Y]=find(B1{1}==max(max(B1{1})));
DIFF=[x,y]-[X,Y]
end
.....basically I'm assigning images to a matrix Bii....each image is an element of the matrix...and I'm trying to find the x,y coordinates of the brightest spot on each image and subtracting that of the first frame from each of them...
IT SAYS ERROR IN USING - (the minus sign)...MATRIX DIMENSIONS MUST AGREE.

2 comentarios

Note that this is supposed to be a modification of the following code that does work...
Bii=B1{ii};
for ii= 1:N
Bii=B1{ii};
[x,y]=find(B1{ii}==max(max(B1{ii})));
[x,y]=find(B1{ii}==max(max(B1{ii})))
end
Use {}Code format next time.

Iniciar sesión para comentar.

 Respuesta aceptada

Fangjun Jiang
Fangjun Jiang el 22 de Oct. de 2011
The constructed matrix [x,y] and [X,Y] don't have the same size so you can't do subtraction. see
rand(3,1)-rand(2,4)

29 comentarios

Hi,
I tried
for ii= 1:N
Bii=B1{ii};
[x,y]=find(B1{ii}==max(max(B1{ii})));
[X,Y]=find(B1{1}==max(max(B1{1})));
rand(x,y)-rand(X,Y)
end
AND IT DID WORK...
HOWEVER, I don't understand why the matrix dimensions of x,y and X,Y don't match, since they are essentially the same thing!
x,y is the brightest spot of the general ii th frame and X,Y is the brightest spot of the 1st frame....
Moreover, if my matrix dimensions really don't match, then according to maths, matrix subtraction is not possible if the matrix dimensions don't match...so how does rand function give me an output anyway? Does it manipulate in some way?
also, my matrix has exactly 480 frames, so i should get 479 output values....but I'm getting WAY more than that...i think there's something wrong! I'm not sure if the rand function is doing what its supposed to be doing!!
FYI, rand(x,y) creates a x by y matrix with random numbers. I was providing an example to explain the error message.
[x,y] and [X,Y] are the same only when ii=1, the first iteration in the loop. Whether they are the same size depends on the data. Maybe there are duplicated maximum values. Run your code step by step, check the value of x,y,X,Y and you'll find the problem.
And since [X,Y]=find(B1{1}==max(max(B1{1}))); does not change inside the loop, you might as well do that assignment outside of the loop.
I think it likely that your frames have multiple locations that have the maximum. If you could be sure that that was *not* the case, that you only had a single copy of each maximum, then you would not use that code at all: you would simply call max() with two output arguments as the second output argument will return the location of the maximum.
you're right....in one of the frames that i tested out separately, there were 4 pairs of x,y with the same maximum intensity...
is there any function/ modification of the max function that will allow me to filter out the largest out of all these for...
instead of iterating the max function 2 or 3 times, maybe there's a function that simply gives one single maximum for each frame, which would thus be the ultimate maximum?
I can use CPSELECT to open pairs of images at a time and locate the cell and calculate the difference between its positions...however, its involves manual effort...is it possible to put cpselect into a loop and that the cell gets located without any manual input?
Will [x,y]=find([0 1 0 1 1],1,'first') solve the problem? In fact, if you use two input arguments for find(), you don't need to specify 'first'.
[maxval, maxidx] = max(B1{ii}(:));
[x,y] = idx2sub(size(B1{ii},maxidx);
It does not make sense to talk about "the largest out of all these" where "these" are points that (have been discovered to) have the same intensity. "maximum" is a superlative. Something cannot be "more maximum" than something else.
The code I show above will return *one* of the locations that has the maximum value. If you care to dig in to the documentation for max(), you could even figure out which one amongst equals it would pick out, but it is not clear to me that that is going to matter. I don't think you really even care what the location is: I think all you need is (before the loop)
maxB11 = max(B1{1}(:));
and then in the loop
max(B1{ii}(:)) - maxB11
I PUT:
M=mmreader('cell.avi');
N= M.NumberOfFrames;
for ii = 1:N
A=imsubtract(M.read(1),M.read(ii));
B{ii}=rgb2gray(A);
[maxval, maxidx] = max(B{ii}(:));
[x,y] = idx2sub(size(B{ii},maxidx));
[x,y] = idx2sub(size(B{ii},maxidx));
end
IT SAID ERROR:
Undefined function 'idx2sub' for input arguments of type 'double'.
WHAT'S WRONG WITH IT?
Sorry should have been ind2sub
PLEASE CHECK..
M=mmreader('cell.avi');
N= M.NumberOfFrames;
for ii = 1:N
A=imsubtract(M.read(1),M.read(ii));
B{ii}=rgb2gray(A);
[maxval, maxidx] = max(B{ii}(:));
[x,y] = ind2sub(size(B{ii},maxidx));
[x,y] = ind2sub(size(B{ii},maxidx));
end
ERROR SHOWN:
Error using ind2sub (line 35)
Not enough input arguments.
ind2sub(size(B{ii}),maxidx);
You had the ) in the wrong place.
I got output! Thanks!
In the mathworks documentation, it says :'The ind2sub command determines the equivalent subscript values corresponding to a single index into an array.'
So in the code, max(B{ii}(:)); gives the X-Y COORDINATES of ONE the maximum intensity points on the picture, and ind2sub(size(B{ii}),maxidx); gives me the indices of that point??
One more thing...my array B has 480 frames...however, in the output, I got 336 columns....is there anything I need to be concerned about here?
The opposite! [MaxValue, MaxValueIndex]=max() gives the linear index (or single index). ind2sub() converts it into subscripts (or X-Y coordinates as you say).
Thanks for the clarification!!
But why do I get 336 out put values whereas I have 480 frames?
What is your output?
The code shown has no outputs, so we don't know.
M=mmreader('cell.avi');
N= M.NumberOfFrames;
for ii = 1:N
A=imsubtract(M.read(1),M.read(ii));
B{ii}=rgb2gray(A);
[maxval, maxidx] = max(B{ii}(:));
ind2sub(size(B{ii}),maxidx);
end
>> max(B{200})
...THATS THE CODE I ENTERED...AND THEN max(B{200})GAVE ME THE FOLLOWING OUTPUT..
ans =
Columns 1 through 17
14 10 8 12 9 8 7 10 10 12 7 5 8 8 6 5 4
Columns 18 through 34
4 10 4 4 4 6 4 4 7 4 7 10 9 9 13 8 11
Columns 35 through 51
10 9 7 6 11 9 7 7 8 5 5 6 7 12 9 6 8
Columns 52 through 68
8 11 6 5 10 7 9 8 9 12 12 11 13 13 10 15 9
Columns 69 through 85
8 7 11 13 15 11 10 10 9 16 13 13 7 13 13 9 10
Columns 86 through 102
11 16 15 11 8 8 14 11 15 24 32 28 13 18 14 11 12
Columns 103 through 119
11 14 9 7 8 10 14 15 8 7 7 8 13 6 11 9 8
Columns 120 through 136
9 5 13 11 10 9 11 13 11 12 12 7 13 9 15 16 8
Columns 137 through 153
9 8 15 13 18 8 7 10 13 14 10 8 7 6 9 13 12
Columns 154 through 170
7 7 11 14 8 6 10 10 16 7 11 13 8 9 13 14 12
Columns 171 through 187
11 7 14 18 8 9 12 11 7 8 7 6 9 8 10 11 11
Columns 188 through 204
12 11 8 9 9 11 12 14 12 12 10 9 14 6 17 10 12
Columns 205 through 221
8 7 8 12 11 11 12 10 12 10 13 12 9 8 8 12 9
Columns 222 through 238
15 13 12 14 9 9 9 12 7 7 10 12 14 7 11 11 8
Columns 239 through 255
20 15 13 12 14 16 10 9 12 8 9 11 10 11 12 8 11
Columns 256 through 272
12 9 13 10 9 8 16 15 9 11 10 8 9 5 6 5 11
Columns 273 through 289
9 12 10 11 9 5 6 8 15 8 7 8 8 7 9 7 8
Columns 290 through 306
6 5 5 4 3 5 6 10 7 5 11 13 9 7 10 9 9
Columns 307 through 323
6 7 5 4 6 6 9 5 4 4 5 6 7 8 4 10 7
Columns 324 through 336
6 5 7 4 5 7 7 10 7 6 5 6 10
>>M
Summary of Multimedia Reader Object for 'cell.avi'.
Video Parameters: 29.97 frames per second, RGB24 336x96.
480 total video frames available.
(SUMMARY OF MY VIDEO FILE WHOSE FRAMES HAVE BEEN STORED IN THE ARRAY)
You have 480 frames. Each frame is 336x96. Maybe it should be 96x336 because if you run a=max(rand(96,336)), a will be 1x336 vector.
Oh no...but for each of the 480 frames, I need just one pair of x,y values that will give me the coordinates of the brightest spot there....even if there are multiple spots of the same max intensity, I just need x,y coordinates of one of them.
Then you need to use max(B{200}(:))! In your code, you are using [maxval, maxidx] = max(B{ii}(:)). That is correct! Why did you run max(B{200})? Where did this 480 mis-match 336 question come from?
You left off the (:) part of the code. max(B{200}(:)) asks for the maximum of B{200} after it has been reshaped to a single column vector. max(B{200}) on the other hand asks to find the maximum of B{200} along each column of B{200}, which will have an output that has as many elements as B has columns.
Hey thanks! this works...I get the maximum intensity...
However, what i need is the x,y coordinates of this point...
I tried [x,y]=find(max(B{200}(:)))
But even if I replace 200 by some other value, i get x=1, y=1!!
That is not necessarily a problem. Maybe your data is like that. What you really need may be this.
x=zeros(480,1); y=zeros(480,1); % This is pre-allocate the X-Y for each frame.
Inside the for-loop, use [x(ii),y(ii)] = ind2sub(size(B{ii}),maxidx). That will give you all the X-Y.
Thanks a ton!!!! It works just fine!
Good to know! It took a long way, 26 comments.
yeah! But then all's well that ends well...hopefully this should bring an end to my project too...hope he doesn't ask me to modify it too much!!
I did say hours ago that the code shown had no outputs. You were overwriting [x,y] each pass through the loop, and you were not doing anything calculated indices.
Memo for future reference: anything that appears un-indexed on the left-hand side of an assignment in a "for" loop will be overwritten on the next iteration. If you want the results to be saved for after the loop, you need an indexed assignment at some point in the loop.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Programming en Centro de ayuda y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by