MATLAB Answers

Having problems with saving vectors in a loop

1 view (last 30 days)
Aliyu Abdu
Aliyu Abdu on 10 May 2012
I want to save the vectors "c" and "r" for each iteration in the loop(code below). Any suggestions please?
lines = houghlines(rotI,theta,rho,P,'FillGap',5,'MinLength',25);
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
a = (xy-1);
r = (xy+1);
c = [a(1) a(2) r(2) r(1)];
r = [a(3) a(4) r(4) r(3)];
end
The two vectors "c" and "r" are both 1x4 in the first iteration. When I tried using "c(k),r(k)" it returns the error: "In an assignment A(I) = B, the number of elements in B and I must be the same". And when I used "c(k,:),r(k,:)", it gave me the error message: "Subscripted assignment dimension mismatch". The thing is "c" and "r" will increase in size at each loop iteration; and want it to be such that when k=1: c&r = 1x4 k=2: c&r = 2x4....k=n: c&r = nx4 How do I save c & r? Please help.

  0 Comments

Sign in to comment.

Accepted Answer

Aliyu Abdu
Aliyu Abdu on 11 May 2012
Since the vectors "c & r" change row size in each loop iteration:
c = zeros(length(lines),4); %Initialization(in my case n is constant=4)
r = zeros(length(lines),4);
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
a = (xy-2);
b = (xy+2);
c(k,:) = [a(1) a(2) b(2) b(1)]*1^k;%Simply to introduce "k" in the... %...assignment.
r(k,:) = [a(3) a(4) b(4) b(3)]*1^k;
end

  1 Comment

Aliyu Abdu
Aliyu Abdu on 12 May 2012
Thanks to: http://www.mathworks.com/matlabcentral/answers/contributors/869436-doug-hull

Sign in to comment.

More Answers (1)

Thomas
Thomas on 10 May 2012
This video should help:
Eg>
for jj = 1:500
B(jj)=rand(1);
end
B will be a vector of size 500
In you case it should be easy to save c and r as vector using c(k),r(k) or if you have those as arrays as c(k,:) and r(k,:)

  1 Comment

Aliyu Abdu
Aliyu Abdu on 11 May 2012
The two vectors "c" and "r" are both 1x4 in the first iteration.
When I tried using "c(k),r(k)" it returns the error: "In an assignment A(I) = B, the number of elements in B and I must be the same".
And when I used "c(k,:),r(k,:)", it gave me the error message: "Subscripted assignment dimension mismatch".
The thing is "c" and "r" will increase in size at each loop iteration;
and want it to be such that when k=1: c&r = 1x4
k=2: c&r = 2x4
.
.
k=n: c&r = nx4
How do I save c & r? Please help.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by