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 (최근 30일)
이전 댓글 표시
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
댓글 수: 1
채택된 답변
Guillaume
2018년 5월 30일
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.
댓글 수: 0
추가 답변 (1개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!