How can I access a submatrix as an array inline?

1 回表示 (過去 30 日間)
Sean
Sean 2011 年 6 月 30 日
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

採用された回答

Sean de Wolski
Sean de Wolski 2011 年 6 月 30 日
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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFunction Creation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by