MATLAB Answers

Turning a product of two vectors into a matrix without using for loops?

1 view (last 30 days)
I have to two vectors x and y, each of length n. I want a matrix M where the elements of M are . How can I construct M on matlab without using a for loop?

Accepted Answer

Star Strider
Star Strider on 8 Oct 2021
I believe something like this should do what you want —
x = 1:10
x = 1×10
1 2 3 4 5 6 7 8 9 10
y = 11:20
y = 1×10
11 12 13 14 15 16 17 18 19 20
M = x(:)*y
M = 10×10
11 12 13 14 15 16 17 18 19 20 22 24 26 28 30 32 34 36 38 40 33 36 39 42 45 48 51 54 57 60 44 48 52 56 60 64 68 72 76 80 55 60 65 70 75 80 85 90 95 100 66 72 78 84 90 96 102 108 114 120 77 84 91 98 105 112 119 126 133 140 88 96 104 112 120 128 136 144 152 160 99 108 117 126 135 144 153 162 171 180 110 120 130 140 150 160 170 180 190 200
Using ‘x’ as x(:) forces it to become a column vector. The rest is straightforward vector-matrix multiplication.
.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by