How can i multiply every row to row values?

5 次查看(过去 30 天)
I am looking for a way to multiply one row values with every row values.
Let's assume we have:
A=[1 2 3;
4 5 6;
7 8 9]
B=[a b c]
I am looking for a way to have:
C=[1*a 2*b 3*c;
4*a 5*b 6*c;
7*a 8*b 9*c]
Thanks in advance for your comments.

采纳的回答

dpb
dpb 2021-1-3
>> A=reshape(1:9,3,[])
A =
1.00 4.00 7.00
2.00 5.00 8.00
3.00 6.00 9.00
>> B=1:3;
>> A.*B
ans =
1.00 8.00 21.00
2.00 10.00 24.00
3.00 12.00 27.00
>>
Works after TMW implemented automagic vector expansion -- I don't recall which release but has been a few cycles now.
If the above doesn't work on your version, use bsxfun
  12 个评论
dpb
dpb 2021-1-5
That's what I just did above except for the full array -- the row of logical True shows every element matches to machine precision.
>> load y_send.mat
>> c1=ones(1,4);
>> demultiplex_data_y1=bsxfun(@times,c1,y_send);
>> all(y_send(:)==demultiplex_data_y1(:))
ans =
logical
1
>>
Explicitly using the variables instead...same result.
>> demultiplex_data_y1([1:5 end-5:end],:)
ans =
-3.00 1.00 -1.00 -1.00
-3.00 1.00 -1.00 -1.00
-3.00 1.00 -1.00 -1.00
-3.00 1.00 -1.00 -1.00
-3.00 1.00 -1.00 -1.00
1.00 1.00 -1.00 3.00
-1.00 -1.00 -3.00 1.00
-1.00 -1.00 -3.00 1.00
-1.00 -1.00 -3.00 1.00
1.00 -3.00 -1.00 -1.00
1.00 -3.00 -1.00 -1.00
>> y_send([1:5 end-5:end],:)
ans =
-3.00 1.00 -1.00 -1.00
-3.00 1.00 -1.00 -1.00
-3.00 1.00 -1.00 -1.00
-3.00 1.00 -1.00 -1.00
-3.00 1.00 -1.00 -1.00
1.00 1.00 -1.00 3.00
-1.00 -1.00 -3.00 1.00
-1.00 -1.00 -3.00 1.00
-1.00 -1.00 -3.00 1.00
1.00 -3.00 -1.00 -1.00
1.00 -3.00 -1.00 -1.00
>>
First and last rows of both arrays -- identical as all() already told us.
Whatever you're looking at, bsxfun isn't the problem.
Or, if you can run the above test and show that
all(y_send(:)==demultiplex_data_y1(:))
returns anything other than a logical TRUE (1), then you have uncovered a bug in your release of MATLAB and should upgrade (which really should do, anyways).
dpb
dpb 2021-1-5
编辑:dpb 2021-1-5
Try the following modification to your program and see what happens there...
load('y_send.mat');
y_send;
bit = 4;
levels = 16;
Fs = 44100;
len = length(y_send);
t = 0:1/Fs:(len-1)/Fs;
max_x = 1;
min_x = -1;
step=(max_x-min_x)/levels;
c1 = [ 1 1 1 1 ];
%row to row and bit to bit multiplication of y_send and c1.
demultiplex_data_y1 = bsxfun(@times, c1, y_send);
demultiplex_data_y1;
if any(demultiplex_data_y1(:)~=y_send(:))
disp('ERROR! bsxfun() times failure with unity multiplier')
else
disp('SUCCESS! bsxfun() times with unity multiplier returns original')
end
...
Running it here resulted in
>> receive_cdm
SUCCESS! bsxfun() times with unity multiplier returns original
>>

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

产品


版本

R2014b

Community Treasure Hunt

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

Start Hunting!

Translated by