Complete newbie with hopefully simple matrix division question - not answered elsewhere that I could find

2 Ansichten (letzte 30 Tage)
Hi everyone,
I am very inexperienced with Matlab (moving to Matlab from Excel because I'm starting to have complicated matrix problems to solve). I have a 17 x 17 matrix that I want to do a division operation on in which I divide each element in the matrix by every other element in the matrix.
I can build it up one matrix at a time, like this...
a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17] b = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17] and so on through c,d,e,f,g,h,i,j,k,l,m,n,o,p,q
a_result = [a(1)/a(1), a(1)/a(2), a(1)/a(3), a(1)/a(4), a(1)/a(5), a(1)/a(6), a(1)/a(7), a(1)/a(8), a(1)/a(9), a(1)/a(10), a(1)/a(11), a(1)/a(12), a(1)/a(13), a(1)/a(14), a(1)/a(15), a(1)/a(16), a(1)/a(17);a(2)/a(1), a(2)/a(2), a(2)/a(3), a(2)/a(4), a(2)/a(5), a(2)/a(6), a(2)/a(7), a(2)/a(8), a(2)/a(9), a(2)/a(10), a(2)/a(11), a(2)/a(12), a(2)/a(13), a(2)/a(14), a(2)/a(15), a(2)/a(16), a(2)/a(17) ... ; a(1)/b(1), a(1)/b(2), a(1)/b(3)
-and so on until I have a matrix of 289 x 17 elements, then do over for vectors b-q...
b_result = [b(1)/a(1), b(1)/a(2), b(1)/a(3), b(1)/a(4), b(1)/a(5), b(1)/a(6), b(1)/a(7), b(1)/a(8), b(1)/a(9), b(1)/a(10), b(1)/a(11), b(1)/a(12), b(1)/b(13), b(1)/a(14), b(1)/a(15), b(1)/a(16), b(1)/a(17);b(2)/b(1), b(2)/b(2), b(2)/b(3), b(2)/b(4), b(2)/b(5), b(2)/b(6), b(2)/b(7), b(2)/b(8), b(2)/a(9), b(2)/b(10), b(2)/b(11), b(2)/b(12), b(2)/b(13), b(2)/b(14), b(2)/b(15), b(2)/b(16), b(2)/b(17)...
then consolidate the 17 matrices into one mega-matrix so that I can do comparative operations on them with 5 other similarly constructed mega-matrices.
...but it looks like a seriously tedious way to do something that probably Matlab can do easily in some way that I don't know. I tried the element-wise operator (./) but I can't work out how to use it in this situation.
Any thoughts?
Thanks, Sabrina

Antworten (2)

KSSV
KSSV am 5 Okt. 2017
a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17] ;
b = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17] ;
A = repmat(a',1,17) ;
%%divide
A1 = A./a ;
A2 = A./b ;
iwant = [A1 ; A2] ;
  1 Kommentar
Sabrina
Sabrina am 5 Okt. 2017
Bearbeitet: Sabrina am 6 Okt. 2017
Thank you very much for your quick response. It will take me a little while to work with your answer and my problem but I'll come back later to let you know how I went. In the meantime I appreciate you giving me a much better way to work with my problem! Thanks!
Update: The repmat function does the job, thanks. Although to get the figures I expect I needed to change the divide command to A1 = A./A'. Still working on it ... more later.

Melden Sie sich an, um zu kommentieren.


Guillaume
Guillaume am 5 Okt. 2017
A note on terminology that will make it easier for you to get understood when asking question, an array is an n-dimension bunch a numbers. A matrix is a 2-dimension array, and a vector is a 1-d dimensional array. So what you've shown in your examples, we tend to call them vectors.
What you want looks very similar to a kronecker tensor product except you want a division instead of a multiplication. Not a problem, just invert your vector (memberwise):
A = 1:17;
a_result = kron(A, 1./A);
a_result = reshape(A, numel(A), []).'; %if you want to arrange it as in your question

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by