Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

What is the error, attempt to grow an array along ambiguous dimension?

Asked by Billy on 3 Dec 2012

Hello, what is the error attempt to grow an array along ambiguous dimension? And, how would I be able to fix that. The code that gives the error is.

    for j = 1:rep
    canv2 = canv;
    del = j*inc;
    canv2(1+del:row+del,1+del:col+del,:) = cm;
    filter = find(canv2 > 0);
    canv3 = bg; 
    canv3(filter) = canv2(filter); 
    image(canv3)
    pause(0.05)
end

3 Comments

Matt J on 3 Dec 2012

Incidentally, there's no apparent reason to use the FIND command in this situation. It would be less computation to just do

 filter=(canv2>0);
 canv3(filter) = canv2(filter); 

assuming canv3 and canv2 are the same size.

Billy on 3 Dec 2012

The error apparently involves line of canv3(filter)=canv2(filter); and I tried changing that line of code and the error still came up. I'll try checking the size.

Jan Simon on 3 Dec 2012

@Billy: Please do not only mention, that you have changed the code, but show us the new code also. We do not have crystal balls (except for Walter, of course).

Billy

Products

No products are associated with this question.

3 Answers

Answer by Matt J on 3 Dec 2012
Edited by Matt J on 3 Dec 2012

Copy/paste the error messages, so we can see what lines generate them and also the output of WHOS so we can see the sizes of all your variables.

My guess would be that it occurs in this line

 canv3(filter) = canv2(filter);

and that it occurs because "filter" contains indices greater than numel(canv3).

1 Comment

Billy on 4 Dec 2012

Yes, that appears to be true. How could I make the number of elements the same?

numel(filter)
ans =
     3959856
 numel(canv3)
 ans =
     3932160
Matt J
Answer by Jan Simon on 3 Dec 2012

When canv3 is an array with more than 1 non-singelton dimension (e.g. 2D matrix or 3D), using a linear or logical index for growing cannot be interpreted uniquely anymore:

x = [1,2; 3,4];
x([true, true, true, true, true, true]) = 7;

Does this create the same error message? If so, Matlab cannot decide, if the new array should be [2x3] or [3x2].

  1. Avoid using the name "filter" for variables, because it is a built-in function
  2. Let canv3 grow explictly at first:
index = (canv2 > 0);  % not "filter"
s = size(canv2);
if any(size(canv3) < s)
  canv3(s(1), s(2), s(3)) = 0;  % Grow
end
canv3(index) = canv2(index);

4 Comments

Jan Simon on 4 Dec 2012

Where do you get the error now? Please post a copy of the complete message.

Billy on 4 Dec 2012

I get the error where canv3(index)=canv2(index).

 Attempt to grow array along ambiguous dimension.
 Error in imageTestTranslate (line 54)
    canv3(index) = canv2(index);
Matt J on 4 Dec 2012

You cannot use linear indexing or other kinds of 1-dimensional indexing to grow an array that is not a vector.

Jan Simon
Answer by Matt J on 4 Dec 2012
Edited by Matt J on 4 Dec 2012

If the whole point of all this is to translate an image by an integer amount, just use circshift

 canv3=circshift(canv2,shiftvector) + bg;

or the function below if you want non-circulant shifting.

    function [B,src_indices,dest_indices]=noncircshift(A,offsets)
    %Like circshift, but shifts are not circulant. Missing data are filled with
    %zeros.
    %
    %  [B,src_indices,dest_indices]=noncirchift(A,offsets)
    %
    %B is the resulting array and the other outputs are such that
    %
    %  B(dest_indices{:})=A(src_indices{:})
    siz=size(A);
    N=length(siz);
    if length(offsets)<N
       offsets(N)=0; 
    end
    B=zeros(siz);
    indices=cell(3,N);
    for ii=1:N
        for ss=[1,3]
         idx=(1:siz(ii))+(ss-2)*offsets(ii);
          idx(idx<1)=[];
          idx(idx>siz(ii))=[];
         indices{ss,ii}=idx;
        end
    end
    src_indices=indices(1,:);
    dest_indices=indices(3,:);
    B(dest_indices{:})=A(src_indices{:});

0 Comments

Matt J

Contact us