How can i multiply every row to row values?

조회 수: 5 (최근 30일)
Noman Abir
Noman Abir 2021년 1월 3일
편집: dpb 2021년 1월 5일
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