How can I access a submatrix as an array inline?

3 views (last 30 days)
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 (51x51 matrix input), while a2 & b2 are a single histogram of the full submatrix dataset (51x51=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

Accepted Answer

Sean de Wolski
Sean de Wolski on 30 Jun 2011
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

More Answers (0)

Community Treasure Hunt

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

Start Hunting!