How do I get the min and max of an array with this error "In an assignment A(:) = B, the number of elements in A and B must be the same."

2 vues (au cours des 30 derniers jours)
Greetings I was able to calculate mean2() and std2() without issues. Then when I attempted min() and max() I now get this error
"In an assignment A(:) = B, the number of elements in A and B must be the same."
I looked at the array output for mean2() and std2() it looks like this (example)
48 0 0 0 0 0
42 0 0 0 0 0
43 0 0 0 0 0
46 0 0 0 0 0
49 0 0 0 0 0
52 0 0 0 0 0
I think the min() and max(), does not know how to give me the answer so I have tried several variations of min and max hoping that it would work
Here is my mean2() and std() codes
plantMeans(i) = mean2(nonzeros(SV_crop));
plantStd(i) = std2(nonzeros(SV_crop));
Here is what I have tried in both the min() and max() with no success
%plantMin(i) = min(SV_crop(:)); %6x1
%plantMin(i) = min(SV_crop);
%plantMin(i) = min(nonzeros(SV_crop(:)));
%plantMin(i) = min(nonzeros(SV_crop));
%plantMin
%plantMin(i) = min(nonzeros(SV_crop,[],1)); returns a row vector containing the smallest element in each column
%plantMin(i) = min(SV_crop,[],1);
%plantMin(i) = min(SV_crop,[],6);%[] dim returns a row vector containing the smallest element in each column
%plantMin(i) = min(SV_crop,[],'omitnat'); %ignores NaN
%plantMin(i) = min(SV_crop,[],'includenat'); %accpts NaN
%plantMin(i) = (SV_crop == min(SV_crop(:)));
%2d matrix------------
%plantMin(i) = min(min(nonzeros(SV_crop)));
%plantMin(i) = min(min(SV_crop));
%3d matrix-------------
%plantMin(i) = min(min(min(nonzeros(SV_crop))));
%plantMin(i) = min(min(min(SV_crop))));
I could really use some help/ suggestions Thank you

Réponse acceptée

Guillaume
Guillaume le 30 Mai 2018
Here is what I have tried with no success
Perhaps trying to understand how the functions work, particularly with regards to dimensions, would be more worthwhile than blindly trying random syntax.
An explanation of no success is also needed. For example
plantMin(i) = min(SV_crop(:));
is not going to result in the error mentioned. It may not give the result you wanted but you haven't told us anything about that (size of input, desired size of output) so we can't tell you what the proper syntax.
It is also unclear why you're trying with and without nonzeros. Do you or do you not want zero values included? Similarly, it makes no sense to try both 'omitnan' and 'includenan'. You either want or don't want the nans to be taken into account. If there is no nans, then the options have no effect.
If SV_crop is a matrix and you want the global minimum of the matrix, then
plantMin(i) = min(SV_crop(:));
is the correct syntax. It reshapes the matrix into a vector (with the |(:)) and take the minimum of that vector. Another syntax that will produce the exact same result, for 2D matrices only:
plantMin(i) = min(min(SV_crop));
which takes the minimum of each column to produce a row vector, then take the minimum of that vector.

Plus de réponses (1)

Rae Pond
Rae Pond le 4 Juin 2018
Apologies for the late response. Thank you for helping me out, your code made sense and it works. For some reason, the last cropped image does not contain any plant so the pixels are 0. I think this is causing the problem.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by