for loop/functions
Show older comments
Hi
I'm stuck on a certain problem I'm trying to solve.
I'm running a for loop with an array of 90 values. These values are passed to a function I created. I need to record every value that the function produces (I'm thinking within an array). I just cannot seem to work out how to do that.
I also have another for loop running with another 90 values, passed to the same function and need to do the same with these values.
The 2 sets of data then need to be put into a 2D array (I also can't seem to work this part out either). Am I best getting each data set into separate arrays first? Then into a 2D array? Am I also correct that these two data sets cannot be put into a 2D array unless they are the same size?
Thanks
Bob
1 Comment
Robert Cumming
on 30 Nov 2011
show us what you;ve tried - you'll get better help that way.
Answers (1)
Wayne King
on 30 Nov 2011
There's no reason why you can't assign the output of a function to an array
% initialize an array
A = zeros(100,2);
for nn = 1:100
A(nn,1) = times(nn,nn+1);
A(nn,2) = sum(1:nn);
end
Is this what you asking?
4 Comments
Wayne King
on 30 Nov 2011
function() outputs a scalar? B is a number. Also, function() is only set up to take r as a scalar?
Wayne King
on 30 Nov 2011
You didn't answer my question about B, if B is a scalar. Still it sounds like what I suggested works:
put your r values in a vector, let's assume it has length 100
B = zeros(100,1);
for nn = 1:100
s = r(nn)/2;
B(nn) = function(r(nn),angle,s);
end
max(B)
Wayne King
on 30 Nov 2011
Bob, in your code example you showed inputting the angle as a vector. does your function only accept a single angle at a time? Then do this.
B = zeros(91,1);
anglez = 0:90;
for nn 1:91
B(nn) = function(r,anglez(nn),s);
end
Wayne King
on 30 Nov 2011
Given that I know absolutely nothing about your function(), I cannot possibly speculate on what the function is doing.
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!