필터 지우기
필터 지우기

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 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