MATLAB: How to access a submatrix as an array inline

efficiencyinline()output formatsubmatrix

I am trying to avoid adding extra/unneeded variables and code to my matlab scripts.
I have noticed that when I access a submatrix inline, I will get a different result than if I make a separate variable of the submatrix beforehand and use the colon operator in my reference.
For Example: -Let A be a 576x720x3 matrix. B=A(50:100,50:100,1); [a1,b1]=histc(A(50:100,50:100,1),0:255); [a2,b2]=histc(B(:),0:255);
This results in a case where a1 != a2 and b1 != b2. They have the same basic values, but a1 & b1 appear to be groups of histograms of smaller data sets (51×51 matrix input), while a2 & b2 are a single histogram of the full submatrix dataset (51×51=2601 elements).
Does anyone know how to get the [a2,b2] result without using the extra variable B? (i.e. is there a way to use an inline reference to a submatrix of A that returns the data as a single column?)
Thanks, Sean

Best Answer

The problem is that B(:) is not the same shape as B which is equal to A(..).
To fix:
A = imread('peppers.png');
B=A(50:100,50:100,1);
[a1,b1]=histc(reshape(A(50:100,50:100,1),[],1),0:255);
[a2,b2]=histc(B(:),0:255);
isequal(a1,a2)
isequal(b1,b2)
reshape A(..) before the call to histc
Related Question