MATLAB
allows to index arrays with other arrays.
Usually the result has the same size, but apparently this is not always the case.
It must have to do with "column priority" in MATLAB
.
QUESTION:
I am wondering how to find a consistent solution on how to get [1 1]
in
size(index_array)==size(array(index_array))
for ALL sizes of index_array
.
To be specific I post a sample script:
baseAlt = [ 0, 11000, 20000, 32000, 47000, 51000, 71000 ];
xRow = 10000*[ 1, 2, 3, 4, 5];
xCol = 10000*[ 1; 2; 3; 4; 5];
xSq = 20000* rand(5);
reg = arrayfun( @(alt) sum(alt>=baseAlt), xRow);
ans1 = baseAlt(reg);
disp(all(size(baseAlt(reg))==size(reg))); %ans=2
reg = arrayfun( @(alt) sum(alt>=baseAlt), xSq);
ans2 = baseAlt(reg);
disp(all(size(baseAlt(reg))==size(reg))); % ans=2
%BUUUUT
reg = arrayfun( @(alt) sum(alt>=baseAlt), xCol);
ans3 = baseAlt(reg);
disp(all(size(baseAlt(reg))==size(reg))); % row, instead of column
% zero when comparing size
%MOREOVER
reg = arrayfun( @(alt) sum(alt>=baseAlt), xCol);
ans4 = baseAlt([reg,reg]);
disp(all(size(baseAlt([reg,reg]))==size([reg,reg]))); % we get true here
As you can see, all the matrices retain the shape of the indexing matrices, APART from the case when a ROW gets indexed by a COL. Could anyone shed light on this please?
EDIT 1: Used "all" instead of "sum", which, as Dani pointed out, is much more elengant here.