Nested for loop help needed

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 Kommentare

Cedric
Cedric am 31 Okt. 2013
Bearbeitet: Cedric am 31 Okt. 2013
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
Tom am 31 Okt. 2013
Yes I see what you're saying, I'll give that a go. Thanks
Tom
Tom am 31 Okt. 2013
Bearbeitet: Tom am 31 Okt. 2013
I'm still struggling, Do I really not need to use any loops? I feel like I'm getting close with:
for j = 2:(n-1)
for
dotp(count1) = sum(x(j,:).*x(3,:))
count1 = count1 + 1
end
end
But the *x(3,:) term isn't right.
n = number of columns by the way.
Any thoughts?
Cedric
Cedric am 31 Okt. 2013
Bearbeitet: Cedric am 31 Okt. 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
Tom am 31 Okt. 2013
It's not for homework per se, I am working on an algorithm which I need to use for my masters.
I have already implemented the element-wise multiplication of vectors as you can see in my above comment, thanks to your previous guidance. The problem I am having now is implementing (what I believe will ultimately be) a nested for loop to perform the sequence described in my original post. I am about to have another go at it, with hopefully more success than yesterday, but if you would know how to do it at a glance, I would be grateful if you could let me know.
Kind regards,
Tom
Image Analyst
Image Analyst am 31 Okt. 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.
Cedric
Cedric am 31 Okt. 2013
Bearbeitet: Cedric am 31 Okt. 2013
The way I understand it is that this thread is about combinations and the one that you mention is about "all rows until the end" .. but I might have misunderstood.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Cedric
Cedric am 31 Okt. 2013
Bearbeitet: Cedric am 31 Okt. 2013

1 Stimme

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.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Gefragt:

Tom
am 30 Okt. 2013

Bearbeitet:

am 31 Okt. 2013

Community Treasure Hunt

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

Start Hunting!

Translated by