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

4 visualizzazioni (ultimi 30 giorni)
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 Commenti
Billy
Billy il 3 Dic 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
Jan il 3 Dic 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).

Accedi per commentare.

Risposte (3)

Jan
Jan il 3 Dic 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 Commenti
Billy
Billy il 4 Dic 2012
Modificato: Billy il 4 Dic 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
Matt J il 4 Dic 2012
Modificato: Matt J il 4 Dic 2012
You cannot use linear indexing or other kinds of 1-dimensional indexing to grow an array that is not a vector.

Accedi per commentare.


Matt J
Matt J il 3 Dic 2012
Modificato: Matt J il 3 Dic 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 Commento
Billy
Billy il 4 Dic 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

Accedi per commentare.


Matt J
Matt J il 4 Dic 2012
Modificato: Matt J il 4 Dic 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{:});

Community Treasure Hunt

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

Start Hunting!

Translated by