MATLAB Computing question 1st year Engineering-MATLAB

AI Thread Summary
To calculate all possible combinations of two vectors in relation to the Pythagorean theorem in MATLAB, a nested loop is recommended. The user initially attempted to compute the hypotenuse using element-wise operations, which limited the results to direct pairings of corresponding elements. Instead, by iterating through each value of the 'opposite' vector within an outer loop and each value of the 'adjacent' vector within an inner loop, all combinations can be evaluated. This approach allows for calculations like adjacent=1 with opposite=2, rather than just corresponding pairs. Utilizing matrix operations such as inner and outer products could also be explored, but a straightforward loop is suggested as the simplest method to achieve the desired results.
Bostonpancake0
Messages
42
Reaction score
0
Hi,

the question is as follows.

I wish to calculate all of the different possible combinations of two vectors in regards to Pythagoras theorem. Not just an element by element operation

My code is as follows;

opposite=(0:30)
adjacent=(0:30)
hypotenuse=sqrt(x1.^2+y1.^2)

When I use this code it only gives me distances using element by element operation. How might one prevent this from occurring and consider all combinations of adjacent and opposite side lengths?
 
Physics news on Phys.org
Bostonpancake0 said:
Hi,

the question is as follows.

I wish to calculate all of the different possible combinations of two vectors in regards to Pythagoras theorem. Not just an element by element operation

My code is as follows;

opposite=(0:30)
adjacent=(0:30)
hypotenuse=sqrt(x1.^2+y1.^2)

When I use this code it only gives me distances using element by element operation. How might one prevent this from occurring and consider all combinations of adjacent and opposite side lengths?

It's not clear what you mean by 'all of the different possible combinations of two vectors in regards to Pythagoras theorem.' Are you talking about two vectors which have only integer lengths?
 
Do you mean you want a 31x31 matrix of the lengths? Use a nested loop
 
sorry I was not 100% clear in my description.

When I run the given code above ( x1 and y1 are adjacent and opposite, forgot to change them), the hypotenuse matrix that returns, only considers element by element computations with the two given matrices adjacent and opposite (i.e. only 30 elements in length).

Thus my code will only produce for example: adjacent=1, opposite=1 thus hypotenuse=sqrt(1^2+1^2)
then adjacent=2 with opposite=2 thus hypotenuse=sqrt(2^2+2^2)
But let's say I wanted to compute adjacent =1 with opposite=2?
then maybe adjacent=1 with opposite=3?
then maybe adjacent=5 with opposite=2? and so and so forth.

This isn't of any real importance I was considering the question today whilst doing my programming homework.
 
Hint: "inner product"
 
Well, I tried to be cute with the short reply, and I blew it!

New hint: "outer product"
 
Expanding on @mikeph response: Make a loop that runs through the values of opposite and within that loop, make a loop that runs through the values of adjacent. For each combination, calculate the hypotenuse. In your example, where both opposite and adjacent are (1..30), you only have to do half of the inner loop since the answers for swapped opposite/adjacent values are identical and have already been calculated. I don't know if there is a MATLAB shortcut to do that. Maybe someone can assist.
 
You can consider both the inner and outer products as matrix multiplications. You just need to get the matrix dimensions right and the matrices in the right order.
Inner product: ##\begin{bmatrix}1 & 2\end{bmatrix} \begin{bmatrix}3 \\ 4\end{bmatrix} = \begin{bmatrix} 11 \end{bmatrix}##
Outer product: ##\begin{bmatrix}1 \\ 2\end{bmatrix} \begin{bmatrix}3 & 4\end{bmatrix} = \begin{bmatrix}
3 & 4 \\ 6 & 8 \end{bmatrix}##

They are both common operations in numerical analysis and both should be available in Matlab. The inner product is sometimes called a "scalar" or "dot" product, and the outer product is sometimes called a "rank 1 update" to a matrix.

Finding them in Matlab is left as a learning exercise for the OP :smile:

But I don't think that what the OP wants do. Probably calculating the terms in a loop the simplest way.
 

Similar threads

Replies
4
Views
1K
Replies
10
Views
3K
Replies
2
Views
2K
Replies
4
Views
2K
Replies
4
Views
1K
Replies
4
Views
2K
Back
Top