Nested for loop help needed
Mostra commenti meno recenti
Hi guys, can anyone help me out with this one:
x =
A B C
D E F
G H I
J K L
I need to produce a loop which for this matrix, would output 3 numbers defined as:
1st = DG + EF + FI
2nd = DJ + EK + FL
3rd = GJ + HK + IL
1st = the elements of the second row * the elements of the third
2nd = the elements of the second row * the elements of the fourth
3rd = the elements of the third row * the element of the fourth
So you see, the second row is held and multiplied with all rows below it. Then the loop moves down so the third row is held and multiplied with all rows below it.
Also it's required that the loop can accommodate a matrix with any number of columns, but always three rows. If anyone's interested, the number of permutations = ((N-1)(N-2)) / 2 where N = number of columns
Could anyone help me in figuring this out? I've been getting tangled up in loops for a while now, but I think the problem is probably quite simple for someone with experience of such things.
Kind regards,
Tom
8 Commenti
If there are always four rows, why a loop?
x(2,:)
for example is the whole 2nd row of x. You probably know how to perform an element-wise multiplication to get a vector of products between x(2,:) and e.g. x(3,:). Then you can just compute a basic SUM of these terms..
Tom
il 31 Ott 2013
I answer assuming that this is for a homework; let me know if it's not the case. Look at the following
>> x = randi(10, 4, 6)
x =
9 7 10 10 5 7
10 1 10 5 10 1
2 3 2 9 8 9
10 6 10 2 10 10
is a matrix of random numbers.. if you want to extract row 2:
>> x(2,:)
ans =
10 1 10 5 10 1
As you can see, it's a vector.. that you could use in further computations.
Second hint: assume that you have two vectors:
>> a = [3, 2, 4] ;
>> b = [6, 2, 10] ;
you can easily compute the element-wise multiplication of these vectors:
>> c = a .* b
c =
18 4 40
where you can check that each element of c is the product of corresponding (same column) elements in a and b.
Third hint: assume that you have a vector..
>> y = [3, 7, 2, 5, 9, 1] ;
It is easy to get the sum of its elements using function SUM:
>> sum(y)
ans =
27
Tom
il 31 Ott 2013
Image Analyst
il 31 Ott 2013
For something this tiny, I wouldn't worry about loops if it gets the job done. Now if you had thousands of rows and columns, then you might start to worry. But for a few or a few dozen, it's no problem.
Image Analyst
il 31 Ott 2013
Isn't this essentially the same as http://www.mathworks.com/matlabcentral/answers/104490-nested-for-loop-needed?
Risposte (1)
Following up on comments..
We usually try to avoid explicit loops when we can implement a matrix/vector approach. The latter is much more efficient than looping, especially looping over both rows and columns. My hints were showing how to get rid of the loop over columns:
X = ...
number1 = sum( X(2,:) .* X(3,:) ) ;
number2 = sum( X(2,:) .* X(4,:) ) ;
number3 = sum( X(3,:) .* X(4,:) ) ;
This is probably the best approach if you have only 4 rows in X, because it avoids the complication of looping over combinations of {2,3,4} taken 2 at a time. A loop for that would be more complex, probably less efficient, and involve more code.
If X had a variable number of rows, say nRow, and you needed to follow the same approach involving rows 2 to nRow, you could do it as follows:
X = randi(10, 6, 8) ; % Example with 6 rows and 8 columns.
combSet = nchoosek( 2:size(X,1), 2 ) ; % Array of all comb. of 2 elements.
results = zeros( size(combSet, 1), 1 ) ; % Prealloc. for results.
for cId = 1 : length( results )
results(cId) = sum( X(combSet(cId,1),:) .* X(combSet(cId,2),:) ) ;
end
With that, you get (you'll have different numbers due to RANDI)
>> X
X =
9 3 10 8 7 8 7 8
10 6 5 10 8 1 4 8
2 10 9 7 8 3 10 2
10 10 2 1 4 1 1 5
7 2 5 9 7 1 5 5
1 10 10 10 2 9 4 7
>> combSet
combSet =
2 3
2 4
2 5
2 6
3 4
3 5
3 6
4 5
4 6
5 6
>> results
results =
318
257
314
317
200
261
359
168
196
245
Check, taking e.g. combination 3
>> sum( X(2,:) .* X(5,:) )
ans =
314
which is results(3), so it seems to be working.
Categorie
Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!