for loop
Informazioni
Questa domanda è chiusa. Riaprila per modificarla o per rispondere.
Mostra commenti meno recenti
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?
Risposte (3)
Andrei Bobrov
il 30 Nov 2011
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 Commento
Sean de Wolski
il 30 Nov 2011
I think you're missing an operation before the call to sin().
Wayne King
il 30 Nov 2011
0 voti
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 Commenti
Wayne King
il 30 Nov 2011
I do not think you have described where you're getting these outputs from. From two different functions, or from one function?
Wayne King
il 30 Nov 2011
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
il 30 Nov 2011
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
il 30 Nov 2011
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
il 30 Nov 2011
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
il 30 Nov 2011
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 Commenti
Walter Roberson
il 30 Nov 2011
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
il 1 Dic 2011
see my "add"
Walter Roberson
il 1 Dic 2011
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
il 1 Dic 2011
In andrei's code, a is the list of angles, and I is the list of lengths.
Questa domanda è chiusa.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!