# concatenating an output matrix within inner and outer loops

조회 수: 5(최근 30일)
William Campbell 2020년 2월 25일
댓글: William Campbell 2020년 3월 4일
I am inputing data values in an outer loop which are used in calculations in an inner loop. The inner loop runs until a limit is reached whereby I would like to stop the inner loop and save the output data values that have resulted up until the limit has been reached in a matrix.
Then run the next set of values from the outer loop and recalculate another set of values in the inner loop until the limit is reached and append this data to the previous output matrix.
I have tried 'while' and 'for' loops with 'break' and 'continue' statements but so far I have only managed to get a 'k x z' matrix and when the limit is reached it is ignored and the inner loop runs through until the end of t. The step length in t is adjusted to set the resolution of the output in terms of being close to the limit within a certain accuracy.
Is there an easier way of approaching this problem !
k = 1;
for z = 1:10;
in1 = input1(k);
in2 = input2(k);
v=1;
for t = 0:0.1:5; % length of vector t can be varied so allow limit to be
% met in each inner loop
% calculations using in1 and in2 from outer loop
var1(v,k) = a;
var2(v,k) = b;
out = [a b];
limit = norm(out - in1);
% if limit < a constant value then stop inner loop and
% append out to create an Nx2 matrix after the limit is reached
% during every inner loop
v=v+1;
end
z=z+1
end
##### 댓글 수: 1표시숨기기 없음
dpb 2020년 2월 26일
Well, your code has nothing in it to test the limit or break so what else could it do besides run the loop to completion?
a,b are undefined here; we can't tell what's going on w/o knowing what things are.
Your limit variable is just a difference; it will thus be signed depending on which side of the result it's on so testing one-sided will fail half the time (at least).
What is the underlying problem you're trying to solve? If you write the general system, you can probably either solve analytically or use fsolve to return the solution.

댓글을 달려면 로그인하십시오.

### 채택된 답변

Reshma Nerella 2020년 3월 4일
Hi,
In case you only want to break the inner for loop, you can try it this way
output=[];
k = 1;
for z = 1:10;
in1 = input1(k);
in2 = input2(k);
v=1;
for t = 0:0.1:5; % length of vector t can be varied so allow limit to be
% met in each inner loop
% calculations using in1 and in2 from outer loop
var1(v,k) = a;
var2(v,k) = b;
out = [a b];
limit = norm(out - in1);
if limit < a
break;
end
output = [output; out]; % appending data to output matrix
v=v+1;
end
z=z+1
end
In case you want to break both the inner and outer for loops, you can try it this way
output=[];
k = 1;
flag=0;
for z = 1:10;
in1 = input1(k);
in2 = input2(k);
v=1;
for t = 0:0.1:5; % length of vector t can be varied so allow limit to be
% met in each inner loop
% calculations using in1 and in2 from outer loop
var1(v,k) = a;
var2(v,k) = b;
out = [a b];
limit = norm(out - in1);
if limit < a
flag=1;
break; % to break inner loop
end
output = [output; out]; % appending data to output matrix
v=v+1;
end
if flag
break; to break outer loop
end
z=z+1
end
You need not increment z after inner for loop since it gets incremented by 1 in the outer for loop every time it runs. In case you want z to increment by n (number other than 1), you can write it as ‘for z=1:n:10’
##### 댓글 수: 1표시숨기기 없음
William Campbell 2020년 3월 4일
Thanks very much - that's very helpful - I got things working in the end, but your code looks a lot more stream-lined that mine.

댓글을 달려면 로그인하십시오.

R2017a

### Community Treasure Hunt

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

Start Hunting!