for loop
信息
此问题已关闭。 请重新打开它进行编辑或回答。
显示 更早的评论
If I've got two variables, length and angle, and I use a nested for loop in order to calculate area for a range of lengths and angles, how do I populate a 2D array with these values of area?
0 个评论
回答(3 个)
Andrei Bobrov
2011-11-30
r = sort(randi(123,5,1));
a = sort(pi*rand( 5,1));
s = bsxfun(@(r,a)1/2*r.^2.sin(a),r,a.')
or
s = .5*r.^2*sin(a)'
ADD I am understand :)
a = (0:10:90)';
I = 0:.1:.5;
w1 = 1;
S = bsxfun(@times,w1 - I,sind(a)*I);
1 个评论
Sean de Wolski
2011-11-30
I think you're missing an operation before the call to sin().
Wayne King
2011-11-30
0 个投票
This is very similar to your other post:
was your question not answered there? Can't you initialize a 2-D array and fill the array elements?
5 个评论
Wayne King
2011-11-30
I do not think you have described where you're getting these outputs from. From two different functions, or from one function?
Wayne King
2011-11-30
So why does this not work: I'm just using the variable N here
A = zeros(N,2);
for nn = 1:N
A(nn,1) = callfunction( );
A(nn,2) = callfunction( );
end
Walter Roberson
2011-11-30
With I being a vector, it seems plausible that you are returning a vector from each call to a1_01_1104578H_function -- and then trying to store the vector in to a single element of the vector A.
Wayne King
2011-11-30
nn is just the loop index, Walter has asked a very good question, is your function() just outputting a scalar or a vector?
Wayne King
2011-11-30
Is this function available anywhere? Did I remember you saying this was doing trapezoidal integration? If so, why can't you use trapz?
Walter Roberson
2011-11-30
Okay, let's take a more direct approach:
- List your input and output variable names, a brief description of each if not self-evident, and approximate variable sizes. You can use symbols (names) to denote the sizes: just be sure to be consistent so that if two variables have linked sizes, you use the same symbol for each.
- Using the variable names above, and using symbolic indices, show the formula that would be used to construct the output for that combination of indices, complete with output indices
For example, you might show (inventing a bogus formula)
side_len = (Width - 2*Length(J))/2
Area(J,K) = 2*(Length(J) * side_len * sin(K));
4 个评论
Walter Roberson
2011-11-30
Letting L be the array index of the length being used, and letting J being the array index of the angle being used, then would it be correct that
w2(L) = w1 - 2.* l(L);
h(L,J) = l(L) .* sind(angle(J));
A(L,J) = 0.5 .* h(L,J) .* (w1 + w2(L));
with the alternate calculation
A(L,J) = (w2(L) .* h(L,J)) + (h(L,J) .* l(L) .* cosd(angle(J));
If so, then the vectorized form would be:
w2 = w1 - 2 .* l(:);
h = bsxfun(@times, l(:), sind(angle(:).'));
A = repmat( 0.5 .* (w1 + w2), 1, length(angle) ) .* h;
alternate
A = repmat(w2, 1, length(angle)) .* (h .* bsxfun(@times, l(:), cosd(angle(:).')))
Andrei Bobrov
2011-12-1
see my "add"
Walter Roberson
2011-12-1
In my code, columns are in order of the provided lengths, and rows are in order of the provided angles. You probably have sorted the length and angles, but the code I wrote does not assume that.
I will not have time to work on this for a bit as I have some higher priorities for now.
Walter Roberson
2011-12-1
In andrei's code, a is the list of angles, and I is the list of lengths.
此问题已关闭。
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!