matlab: Subscripted assignment dimension mismatch...i can understand why it is showing but cannot get a solution

i am having problem with this code. when i am preallocating Pw then it shows this error
Subscripted assignment dimension mismatch.
i can understand that Pw is a matrix of 1x10000000 and cnt is a matrix of 1x1 but i cannot understand how to solve this
cnt = 1;
%%plane : Xw = 1
Pw = zeros(1, 1000000);%preallocating Pw
for i=0.2:0.2:0.8,%it means it starts from 0.2 gives an interval 0.2 and goes till 0.8
for j=0.2:0.2:0.8,
Pw(cnt,:) = [1 i j];%is the cnt-th row of pw , pw is a matrix whos cnt-th row is a row matrix, cnt is a variable that starts from 1 and after forming
%the row matrix, it goes on till i and j conditions are fulfilled.
cnt = cnt + 1;
end
end
please let me know how to solve this

 Accepted Answer

hi,
you have :
i=0.2:0.2:0.8; % as your loop
The length is 4, then initialize the matrix Pw as :
Pw=zeros(4,3); % if you expect 4 values
%or
Pw=zeros(16,3); % if you expect (1:4,1:4) values

6 Comments

thank you... it is working. but i applied the same thing for another loop. there is some problem showing.
%%surface Xw = 1
cnt = 1;
Pc = zeros(16,3);
p = zeros(16,3);
for cnt = 1:1:16,
Pc(cnt,:) = (R*Pw(cnt,:)' + T)';
p(cnt,:) = [(Ox - Fx*Pc(cnt,1)/Pc(cnt,3)) (Oy - Fy*Pc(cnt,2)/Pc(cnt,3))];
end
plot(p(:,1), p(:,2), 'r+');
axis([0 512 0 512]);
grid;
hold;
this time, it is showing
Subscripted assignment dimension mismatch.
Error in calib (line 82)
p(cnt,:) = [(Ox - Fx*Pc(cnt,1)/Pc(cnt,3)) (Oy - Fy*Pc(cnt,2)/Pc(cnt,3))];
please let me know
the vector " [(Ox - Fx*Pc(cnt,1)/Pc(cnt,3)) (Oy - Fy*Pc(cnt,2)/Pc(cnt,3))]" has more than 3 as length, try :
cnt=1;
n=length([(Ox - Fx*Pc(cnt,1)/Pc(cnt,3)) (Oy - Fy*Pc(cnt,2)/Pc(cnt,3))]);
p=zeros(16,n);
and try again ....
We do not have evidence that that vector would be longer than length 3. If Ox and Oy and Fx and Fy are all scalars, then the vector would have length 2, which would not match the dimension of 3 that is needed for ":" to work.

Sign in to comment.

More Answers (1)

When you use a destination dimension of ":" for an array, then the number of elements you are writing in must match the size of the array in that dimension. You set the second dimension of Pw to be 1000000, but your assignment statement is only writing 3 values instead of that 1000000.
Notice too that your second assignment to Pw would be writing to Pw(2,:) but you only allocated one row for Pw. The assignment will be allowed to go through, but you will not be taking advantage of preallocation.
[I, J] = ndgrid(0.2:0.2:0.8, 0.2:0.2:0.8);
Pw = [ ones(numel(I), 1), I(:), J(:) ];

Categories

Products

Community Treasure Hunt

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

Start Hunting!