Having problems with saving vectors in a loop

1 次查看(过去 30 天)
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.

采纳的回答

Aliyu Abdu
Aliyu Abdu 2012-5-11
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 个评论
Aliyu Abdu
Aliyu Abdu 2012-5-12
Thanks to: http://www.mathworks.com/matlabcentral/answers/contributors/869436-doug-hull

请先登录,再进行评论。

更多回答(1 个)

Thomas
Thomas 2012-5-10
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 个评论
Aliyu Abdu
Aliyu Abdu 2012-5-11
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Time Series Events 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by