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?

回答(3 个)

Andrei Bobrov
Andrei Bobrov 2011-11-30

1 个投票

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 个评论

I think you're missing an operation before the call to sin().
Wayne King
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
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
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
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
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
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
Walter Roberson 2011-11-30

0 个投票

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 个评论

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(:).')))
see my "add"
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.
In andrei's code, a is the list of angles, and I is the list of lengths.

此问题已关闭。

标签

提问:

Bob
2011-11-30

关闭:

2021-8-20

Community Treasure Hunt

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

Start Hunting!

Translated by