How can i multiply every row to row values?

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.

 Risposta accettata

>> 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 Commenti

The code is not working. How can i use bsxfun..? Can you provide me full code..?
Which release of MATLAB to you have? I see the implicit expansion of operators was introduced in R2016b which is a while ago now. Upgrading would bring quite a few other new features as well.
What did you try? Did you look at the documenation and examples for bsxfun?
MATLAB version of mine is 2014b. A.*B is not working in my version. I tried "bsxfun" leter.
bsxfun working fine on my version. But, It is showing correct values when I have a small row numbers. But, when I have a very large row numbers (say, 10,00000) then it's showing me wrong values.
Is there other ways you can help me..?
How would you show it to be wrong with so many values to look at?
I'd think it very unlikely that the results are incorrect owing to the size of the array; more likely you're looking at the wrong values or somesuch.
Show us specifically the pieces of the problem dataset that you think illustrate an error.
I have added 2 different MATLAB files where "receive_cdm.m" is my main code and "y_send.mat" is a file I am getting some values from it and loaded it in main code section.
If you look at the code then I have assigned 1 parameter c1. Forget about other parameters.
The following tasks are explained in the code.
I have done it with proper MATLAB commands and equations.
But, I am getting error when I calculate it manually and matching it with MATLAB calculations. (What I should get and what I am getting from MATLAB)
Can you check my code, please..?
It would be very helpful for me.
>> load y_send.mat
>> y_send(1:10,:)
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
-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
>> c1=ones(1,4);
>> all(bsxfun(@times,c1,y_send)==y_send)
ans =
1×4 logical array
1 1 1 1
>>
Whatever is the issue, it is NOT bsxfun misbehaving and calculating something different for a large array versus small...you'll have to look elsewhere for a logic problem in your code.
I think you didn't get my point.
Thank you for your valuable time.
dpb
dpb il 5 Gen 2021
Modificato: dpb il 5 Gen 2021
Well, show us the point, then; that's the complaint you made.
"bsxfun working fine on my version. But, It is showing correct values when I have a small row numbers. But, when I have a very large row numbers (say, 10,00000) then it's showing me wrong values."
Where does bsxfun show an incorrect value do you think?
That's the only call to bsxfun in the code you provided; although with the given c1 there's no point altho I presume you have some other set of coefficients awaiting in the wings.
Noman Abir
Noman Abir il 5 Gen 2021
Modificato: Noman Abir il 5 Gen 2021
First see the y_send values only. Take a picture on your phone from the very first row to 15 rows (say).
Then, just run my code to find "demultiplex_data_y1" values.
As, c1 elements are all "1" so i should get the same values like "y_send" right..?
Just see those values from the first row and match them with your picture from your phone.
Are those values same..?
Notify me.
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 il 5 Gen 2021
Modificato: dpb il 5 Gen 2021
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
>>

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Mathematics and Optimization in Centro assistenza e File Exchange

Prodotti

Release

R2014b

Tag

Richiesto:

il 3 Gen 2021

Modificato:

dpb
il 5 Gen 2021

Community Treasure Hunt

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

Start Hunting!

Translated by