Reputation: 13
I try to build a script on Octave and I receive this message:
error: script2: =: nonconformant arguments (op1 is 1x1, op2 is 1x10)
error: called from
script2 at line 5 column 1
My script is:
l = 20:29;
m = 30;
for i = 0:9
a(i + 1) = l / m;
end
Can someone help me fix this?
Upvotes: 1
Views: 95
Reputation: 36
Since Octave is capable of doing matrix operations, you don't need the for
loop in the first place.
I would rather write:
l = 20:29;
m = 30;
a = l / m;
This is much more efficient.
Upvotes: 1
Reputation: 114290
Octave allows you to assign to a non-existent name by making a scalar. You can then append to it by assigning to an index that is one past the length.
When you assign to a(1)
, a
is created as a scalar (or 1x1 array). l / m
is 1x10. That is what your error message is telling you.
There are a couple of workarounds. If you want to just accumulate the rows of a matrix, add a second dimension:
a(i + 1, :) = l / m;
If you want columns:
a(:, i + 1) = l / m;
The problem with this approach is that it reallocates the matrix at every iteration. The recommended approcach is to pre-allocate the matrix a
and fill it in:
l = 20:29;
m = 30;
a = zeros(10);
for i = 1:10
a(i + 1, :) = l / m;
end
Upvotes: 2