Multiple matrices in Matlab without recursion

I need to write a Matlab script that does this:

Input - 2 matrices, A (mxn) and D (mx 1). The output is a matrix, C (mxn). C is calculated as follows:

function c = scanrow(a,d)
[rows columns] = size(d);
for i=1:columns
a(i,:) = a(i,:).*d(i);   
end
c = a;
end

The requirement does not use recursion. I have no idea to solve this problem. Glad you can help. Thanks.

+3
source share
5 answers

If the problem is "multiplying each element in each row A with the corresponding element in the corresponding row D", then I would suggest the following:

dd = repmat(d, 1, size(a, 2))
c = a.*dd

Example:

>> a = [1 2; 3 4; 5 6];
>> d = [1 2 3]';
>> dd = dd = repmat(d, 1, size(a, 2))
dd = 
     1   1
     2   2
     3   3
>> a.*dd
ans =
     1   2
     6   8
     15  18
+3
source

, FYI - bsxfun + - , . , :

a = 1:5;
b = magic(5);

c = bsxfun(@times,a,b);

- MATLAB. -pete

+10

( , , . , , bsxfun.)

, , . repmat, . , . , bsxfun, , . . , ,

A = magic(3) + 5

MATLAB , 5 (3). ,

A = magic(4).*2

, MATLAB , . , MATLAB , , . , repmat.

, . , , , , . MATLAB , ,

A = magic(3).*rand(1,3)

repmat. , repmat , . , MATLAB, , , . MATLAB ( ) - , . ( .)

, , (3) -, MATLAB , ? MATLAB , , ? , Singleton, . ( , 1.) , , , . , , . , . , MATLAB , singleton MATLAB? , .

, MATLAB , , MATLAB .

MATLAB, . * *, . , .

BSXFUN (Binary Scalar eXpansion FUNction), , . bsxfun, , . BSXFUN , , . , :

A = rand(5,1);
B = rand(1,3);
C = bsxfun(@plus,A,B);

5x3 A B. , BSXFUN .

, BSXFUN - . , MATLAB :

A = rand(5,1);
B = rand(1,3);
C = A*B;

D = A-B;
E = A^B;

. BSXFUN .

D = bsxfun(@minus,A,B);
E = bsxfun(@power,A,B);
+3

, vectorize() - , →

0

, , , ? :

, a d A ( m-by-n) D ( m-by-1), . d, , 1 , for , . a d. :

c = a;
c(1,:) = c(1,:).*d(1);
0

Source: https://habr.com/ru/post/1708034/


All Articles