0
votes

I am trying my array in a single dimension, but receive the error "Subscripted Assignment Dimension Mismatch". However, I am able to perform the same operation if I make a temporary variable first. Is there a way to do this without creating the temporary (y) variable? Even better, is there a way to do it without looping, by using an index vector?

Thank you.

x = zeros(2,5);
idx = [1 2];

% This doesn't work...
% x(idx,:) = [x(idx,:) 1];

% This doesn't work...
% for i=idx
%     x(i,:) = [x(i,:) 1];
% end

% This works...
for i=idx
    y(i,:) = [x(i,:) 1];
end
x = y;
1
What do you want to do? Grow x in a single dimension with ones? Do x = [x ones(size(x,1),1)]; - chappjc

1 Answers

1
votes

The error is because x(idx,:) is of size 2x5 and 1 of size 1x1, so you can't concatenate the two. Here's the error I get in Octave:

>> [x(idx,:) 1]
error: horizontal dimensions mismatch (2x5 vs 1x1)

You can, however, do this:

>> [x(idx,:) ones(length(idx),1)]
ans =

   0   0   0   0   0   1
   0   0   0   0   0   1

You'll need an intermediate variable though (your y).