필터 지우기
필터 지우기

How to perform elementwise multiplication between two matrices with different size or summation between two matrices with the same size

조회 수: 3 (최근 30일)
Hello, imagine I have the following array:
f = 1:10;
x = -22:1:22;
y = -22:1:22;
[u,v] = meshgrid(x,y);
I wish to perform elementwise multiplication operation between 'f' and 'u' (I mean the final array must have numel(f)*numel(u) elements.) . How can I fo this efficiently (definitely I know how to do this using "for loop". this is time consuming.)? Moreover, Imagine that I want to sum "u" and "v" elementwisely which both of them have the same size (so the final array must have numel(u)*numel(v) elements.) . How to do this in order to have high speed?

채택된 답변

Steven Lord
Steven Lord 2023년 12월 14일
Let's look at the sizes of the arrays in question.
f = 1:10;
x = -22:1:22;
y = -22:1:22;
[u,v] = meshgrid(x,y);
whos f x y u v
Name Size Bytes Class Attributes f 1x10 80 double u 45x45 16200 double v 45x45 16200 double x 1x45 360 double y 1x45 360 double
I wish to perform elementwise multiplication operation between 'f' and 'u' (I mean the final array must have numel(f)*numel(u) elements.)
So do you specifically want the size of the output to be [10 45*45] = [10, 2025]? Since you're using a release that supports implicit expansion this is not that difficult.
A = reshape(f, numel(f), 1).*reshape(u, 1, numel(u));
whos A
Name Size Bytes Class Attributes A 10x2025 162000 double
How can I fo this efficiently (definitely I know how to do this using "for loop". this is time consuming.)? Moreover, Imagine that I want to sum "u" and "v" elementwisely which both of them have the same size (so the final array must have numel(u)*numel(v) elements.) . How to do this in order to have high speed?
So you want to just normally add u and v?
B = u+v;
whos B
Name Size Bytes Class Attributes B 45x45 16200 double
Or do you want to add each element of u to each element of v, not just corresponding elements?
C = reshape(u, numel(u), 1) + reshape(v, 1, numel(v));
whos C
Name Size Bytes Class Attributes C 2025x2025 32805000 double

추가 답변 (1개)

Voss
Voss 2023년 12월 14일
f = 1:10;
x = -22:1:22;
y = -22:1:22;
[u,v] = meshgrid(x,y);
whos
Name Size Bytes Class Attributes cmdout 1x33 66 char f 1x10 80 double u 45x45 16200 double v 45x45 16200 double x 1x45 360 double y 1x45 360 double
Note that u and v are 45x45 matrices, so each has 2025 elements.
Something along these lines may be what you are looking for:
fu = f(:).*u(:).'
fu = 10×2025
-22 -22 -22 -22 -22 -22 -22 -22 -22 -22 -22 -22 -22 -22 -22 -22 -22 -22 -22 -22 -22 -22 -22 -22 -22 -22 -22 -22 -22 -22 -44 -44 -44 -44 -44 -44 -44 -44 -44 -44 -44 -44 -44 -44 -44 -44 -44 -44 -44 -44 -44 -44 -44 -44 -44 -44 -44 -44 -44 -44 -66 -66 -66 -66 -66 -66 -66 -66 -66 -66 -66 -66 -66 -66 -66 -66 -66 -66 -66 -66 -66 -66 -66 -66 -66 -66 -66 -66 -66 -66 -88 -88 -88 -88 -88 -88 -88 -88 -88 -88 -88 -88 -88 -88 -88 -88 -88 -88 -88 -88 -88 -88 -88 -88 -88 -88 -88 -88 -88 -88 -110 -110 -110 -110 -110 -110 -110 -110 -110 -110 -110 -110 -110 -110 -110 -110 -110 -110 -110 -110 -110 -110 -110 -110 -110 -110 -110 -110 -110 -110 -132 -132 -132 -132 -132 -132 -132 -132 -132 -132 -132 -132 -132 -132 -132 -132 -132 -132 -132 -132 -132 -132 -132 -132 -132 -132 -132 -132 -132 -132 -154 -154 -154 -154 -154 -154 -154 -154 -154 -154 -154 -154 -154 -154 -154 -154 -154 -154 -154 -154 -154 -154 -154 -154 -154 -154 -154 -154 -154 -154 -176 -176 -176 -176 -176 -176 -176 -176 -176 -176 -176 -176 -176 -176 -176 -176 -176 -176 -176 -176 -176 -176 -176 -176 -176 -176 -176 -176 -176 -176 -198 -198 -198 -198 -198 -198 -198 -198 -198 -198 -198 -198 -198 -198 -198 -198 -198 -198 -198 -198 -198 -198 -198 -198 -198 -198 -198 -198 -198 -198 -220 -220 -220 -220 -220 -220 -220 -220 -220 -220 -220 -220 -220 -220 -220 -220 -220 -220 -220 -220 -220 -220 -220 -220 -220 -220 -220 -220 -220 -220
uv = u(:)+v(:).'
uv = 2025×2025
-44 -43 -42 -41 -40 -39 -38 -37 -36 -35 -34 -33 -32 -31 -30 -29 -28 -27 -26 -25 -24 -23 -22 -21 -20 -19 -18 -17 -16 -15 -44 -43 -42 -41 -40 -39 -38 -37 -36 -35 -34 -33 -32 -31 -30 -29 -28 -27 -26 -25 -24 -23 -22 -21 -20 -19 -18 -17 -16 -15 -44 -43 -42 -41 -40 -39 -38 -37 -36 -35 -34 -33 -32 -31 -30 -29 -28 -27 -26 -25 -24 -23 -22 -21 -20 -19 -18 -17 -16 -15 -44 -43 -42 -41 -40 -39 -38 -37 -36 -35 -34 -33 -32 -31 -30 -29 -28 -27 -26 -25 -24 -23 -22 -21 -20 -19 -18 -17 -16 -15 -44 -43 -42 -41 -40 -39 -38 -37 -36 -35 -34 -33 -32 -31 -30 -29 -28 -27 -26 -25 -24 -23 -22 -21 -20 -19 -18 -17 -16 -15 -44 -43 -42 -41 -40 -39 -38 -37 -36 -35 -34 -33 -32 -31 -30 -29 -28 -27 -26 -25 -24 -23 -22 -21 -20 -19 -18 -17 -16 -15 -44 -43 -42 -41 -40 -39 -38 -37 -36 -35 -34 -33 -32 -31 -30 -29 -28 -27 -26 -25 -24 -23 -22 -21 -20 -19 -18 -17 -16 -15 -44 -43 -42 -41 -40 -39 -38 -37 -36 -35 -34 -33 -32 -31 -30 -29 -28 -27 -26 -25 -24 -23 -22 -21 -20 -19 -18 -17 -16 -15 -44 -43 -42 -41 -40 -39 -38 -37 -36 -35 -34 -33 -32 -31 -30 -29 -28 -27 -26 -25 -24 -23 -22 -21 -20 -19 -18 -17 -16 -15 -44 -43 -42 -41 -40 -39 -38 -37 -36 -35 -34 -33 -32 -31 -30 -29 -28 -27 -26 -25 -24 -23 -22 -21 -20 -19 -18 -17 -16 -15

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by