Having problems with saving vectors in a loop

1 回表示 (過去 30 日間)
Aliyu Abdu
Aliyu Abdu 2012 年 5 月 10 日
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 Center および File ExchangeTime Series Events についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by