Multiply the ith column of a matrix by element at position i of a vector

Hi, I would like to do something like:

for i=1:cols(M)
     M(:,i) = M(:,i)*v(i)
end

Is there a fast/vectorized way to do this? Is this faster:

M = M*repmat(v,rows(M),1);

Thank you
Misha
0
mk144210 (63)
5/10/2009 3:08:02 PM
comp.soft-sys.matlab 206978 articles. 0 followers. lunamoonmoon (258) is leader. Post Follow

5 Replies
275 Views

Similar Articles

[PageSpeed] 5
On May 10, 11:08=A0am, "Misha Koshelev" <mk144...@bcm.edu> wrote:
> Hi, I would like to do something like:
>
> for i=3D1:cols(M)
> =A0 =A0 =A0M(:,i) =3D M(:,i)*v(i)
> end
>
> Is there a fast/vectorized way to do this? Is this faster:
>
> M =3D M*repmat(v,rows(M),1);

That does not work.

If

A =3D R*M* C

where R and C are diagonal, then

a(i,j) =3D r(i)*m(i,j)*c(j)

i.e., r(i) multiplies each element in the ith row and c(j)
multiplies each element in the jth column. So, if r and c
are vectors,

A =3D diag(r)*M*diag(c)

Hope this helps.

Greg
0
heath (3982)
5/10/2009 3:26:35 PM
% Data
 M = magic(5)
v = [6 7 8 9 10]

% Engine
D1 = bsxfun(@times,M,v);


% Compare to other method
for i=1:size(M,2)
     D2(:,i) = M(:,i)*v(i);
end

all(D1(:)==D2(:))
0
spamanon (2437)
5/10/2009 3:30:03 PM
"Misha Koshelev" <mk144210@bcm.edu> wrote in message <gu6qkh$3ut$1@fred.mathworks.com>...
> Hi, I would like to do something like:
> 
> for i=1:cols(M)
>      M(:,i) = M(:,i)*v(i)
> end
> 
> Is there a fast/vectorized way to do this? Is this faster:
> 
> M = M*repmat(v,rows(M),1);
> 
> Thank you
> Misha

In general, use BSXFUN; if M is square (NxN) you can also use

M * diag(v)

hth
Jos
0
10584 (932)
5/11/2009 6:02:01 AM
"Jos " <#10584@fileexchange.com> wrote in message <gu8f0p$ci1$1@fred.mathworks.com>...

> if M is square (NxN) you can also use
> 
> M * diag(v)
> 

This works even for M is (M x N) rectangle, the only requirement is v has N elements.

Bruno
0
b.luong5955 (6403)
5/11/2009 6:21:17 AM
"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <gu8g4t$pg0$1@fred.mathworks.com>...
> "Jos " <#10584@fileexchange.com> wrote in message <gu8f0p$ci1$1@fred.mathworks.com>...
> 
> > if M is square (NxN) you can also use
> > 
> > M * diag(v)
> > 
> 
> This works even for M is (M x N) rectangle, the only requirement is v has N elements.
> 
> Bruno

Yep! Thanks for correcting me, Bruno.
0
10584 (932)
5/11/2009 6:41:01 AM
Reply:
Similar Artilces:

matrix boolean operation
Hey all, I am trying to speed up many many computations on very large matrices. I am basically performing boolean operations Ok so lets say I have a matrix M that is dimension 3 x 3 x 3 x 50. What I want to do is generate matrix N of dimension 3 x 3 x 3 such that it chooses whatever value from the 3 x 3 x 3 subset of matrix M that has the highest value in the 4th dimension, the size 50 dimension. Thanks in advance, Colin In article <f88d5e$bn7$1@fred.mathworks.com>, Colin Omi <c.monk@microsoft.com> wrote: >Hey all, I am trying to speed up many many computations on very larg...

Multiply
Hi, Ik have a large SAS-dataset with a lot of variables. I have to multiplay each variable with -1. Is there a short way to do this by using a macro? Now i have to do : X1 = X1 * -1 ; X2 = X2 * -1 ; .. .. X100 = X100 * -1 ; Tx it won't be pretty but you could try something likethe code below. Just replace WORK with your library name and SAMPLE with your table name: Proc SQL NoPrint; Select Trim(Name)!!' = '!!Trim(Name)!!'*-1;' INTO : varlist separated by ' ' From Dictionary.columns Where libname = 'WORK' AND memname = 'SAMPLE' ; Quit; Dat...

multiplying matrice rows
How can I to multiply enumerated list of rows of an matrice on following way: To multyply each row (except rows a,b and c)of matrice A withe row a of the same matrice, and replace that row (not a row) with result. rows b and c are last rows, row a is any row in the range 1->b(exclusive) THX Hi, and m = {{1, 2, 3}, {1, 2, 3}, {1, 2, 3}}; amatrix = {{a, b, c}, {a, b, c}, {a, b, c}} MapAt[Dot[amatrix, #] &, m, {2}] with correct postions in MapAt[] does not help? Regards Jens Dragutin Culinovic wrote: > > How can I to multiply e...

Floating point operation count for a complex matrix exponential?
Hi all, I am attempting to estimate the computational load for an iterative algorithm. One step requires the following calculation: B = exp(i*A) where A is an N x N complex double-precision matrix. I would like to find the number of floating point operations required for this procedure. Any pointers to appropriate resources would be very greatly appreciated! TIA, Hal "@(nospam)rmc.ca" <""harry.ingleby\"@(nospam)rmc.ca"> wrote in message news:42431aa7$1@rmcnt03a.rmc.ca... > Hi all, > > I am attempting to estimate the computational load f...

Partitioning on Timestamp with Time zone column in 10g
Hi Is partitioning on Timestamp with Time zone data type possible in 10g? WHen I try the following it reports ORA-03001 unimplemented feature. The DW guide illustrates partitioning only on DATe columns and there is no specific mention of this data type. create table testtbl (id number, tm_date timestamp with time zone ) partition by RANGE (tm_date) ( PARTITION DEC_2008 VALUES LESS THAN (TO_TIMESTAMP_TZ('2009-01-01 11:00:00 -8:00','YYYY-MM-DD HH:MI:SS TZH:TZM')) ) NOCOMPRESS NOCACHE NOPARALLEL MONITORING; But it works for TIMESTAMP WITH LOCAL TIMESTAMP data type column cre...

how to iterate through matrix to retrieve previous column
Greetings, I'm very new to Matlab and not sure about this. I've a matrix [1 2 3 4;8 9 5 6;] Now I want to iterate through above matrix and retrieve the column previous to the current one. So if while iterating we're at column 2 9 then we should retrieve 1 8. Can someone please point me in that right direction? I tried for v = matrix disp(v-1) end but that didn't work. Any help will be greatly appreciated. On Oct 9, 10:52=A0am, Oltmans <rolf.oltm...@gmail.com> wrote: > Greetings, > > I'm very new to Matlab and not sure about this. I've...

Multiplying rows
Hi! Do you know very sophisticated way to multiply rows as shown below, without loops or indexing? I prefer vectorized way to do this at this time. I have matrix Y=[1 2 3;3 4 5] and I'd like to have YY=[1 2 3;1 2 3;4 5 6;4 5 6] Is it possible utilize reshape, repmat or concatenate commands with this issue? br, Harald "Harald P" <harri.puolitaival@yahoo.com> wrote in message <fu4r8p$e94$1@fred.mathworks.com>... > Hi! > > Do you know very sophisticated way to multiply rows as shown > below, without loops or indexing? I prefer vector...

Re: Multiply 2 matrices where one contains differential operators with one that contains functions of x and y #2
ref (me) >This is my solution: > > > A = {{D[#1, x] & , 0 & }, {0 & , D[#1, y] & }, {D[#1, y] & , D[#1, x] > & }} > B = {{x*y, x^3*y, 3*x + y^2}, {2*x, x^4*y, y^2}} > > {rowsA, colsA} = Dimensions[A]; > {rowsB, colsB} = Dimensions[B]; > > r = Table[0, {rowsA}, {colsB}]; (*where the result of A.B goes *) > > For[i = 1, i <= rowsA, i++, > For[j = 1, j <= colsB, j++, > For[ii = 1, ii <= rowsB, ii++, > r[[i,j]] = r[[i,j]] + A[[i,ii]] /@ {B[[ii,j]]} > ] > ...

Content of syscat.procedures.TEXT column gets truncated
Greetings, I usually retrieve the source code of a stored procedure (SP) from TEXT column of syscat.procedures. This TEXT column is defined as CLOB of length (2097152). However, when selecting TEXT from syscat.procedures for one particular SP (of size 15KB in notepad), the source code in the TEXT was truncated. I notice that the content of TEXT column starts with the word 'TEXT' followed by lots of spaces and '-' before the actual source code appears. I saved the content for this SELECT in notepad , its size is only 25KB. Would anyone know why and how to get ...

Matrix concatenation tricks
I have a matrix ,A (n-by-4) .i then create another matrix ( B) from A. My aim is to get a matrix : I=[A(1,:) 3 7 9 10 ; A(2,:) 2 6 11 12 ; A(3,:) 11 15 5 6 ; A(4,:) 10 14 7 8] ; where: A = [1 2 5 6; 3 4 7 8;9 10 13 14;11 12 15 16] ; B=[A(:,[1 3]) ; A(:,[2 4]) ; A(:,[1 2]) ; A(:,[3 4]) ] ' ; C = reshape(B , 8 , [ ] ) ' ; >>>> On Feb 17, 8:16=A0pm, "Qwuasi Moore" <moore.mit.kw...@yahoo.inra.com.init> wrote: > I have a matrix ,A (n-by-4) .i then create another matrix ( B) from A. > My aim is to get a matrix : > I=3D[A(1,:) 3 7 9 1...

Multiply
Hello All, My question may look silly: Why the following program does not print any output? #include <stdio.h> #include <stdlib.h> #include <math.h> int main() { int i,j; i=1000; j=i*i; printf("%d %d\n",i,j); printf("TEST\n"); return(0); } Thanks, MJK MJK <jafarikia@gmail.com> wrote in news:1192241830.668389.300410 @e34g2000pro.googlegroups.com: > Why the following program does not print any output? What environment? Where is stdout pointing? On Oct 12, 10:24 pm, Kenneth Porter <shiva.blackl...@sewingwitch.com> wrot...

differentiate the columns in a matrix
Hi, friends: I have a matrix X with m by n dimension, I would like to know how many different columns are there in this matrix. Is there any more efficicent way to do it rather than comparing each column with other columns? My first idea is to pick up a colum from X, such as X(:,1), and generate a matrix Y (m,n )with n columns and each colum is the same to X(:,1). Then I can use Y-X to identify whether there are dupicates for X(:,1) in X. Delete the duplicates in X and keep on doing previous steps, and finally I will get the distinguished colums in X. But I sitll do not know how to generat...

Writing a matrix in a text file
Hi all... it's me again! :) I have to write a matrix in a text file, but there i something wrong with my code: fid = fopen(outputfile,'w'); fwrite(fid,matrix1,'int8'); fclose(fid); matrix1 is a matrix of integers, but when i open the output file, i see only a lot of strange characters!!! What's wrong? Anyone knows how to solve that? Thank you very much! Bruno try use fprintf function Bruno wrote: > > > Hi all... it's me again! :) > > I have to write a matrix in a text file, but there i something > wrong with my > code: > > fid = ...

Locate rows in table where column value missing (I think!) #2
I have a simple table with the following data: fldYear fldCode1 fldCode2 2000 ABC1 ABC12 2000 ABC1 ABC13 2001 ABC1 ABC12 2002 ABC1 ABC12 2002 ABC1 ABC13 I need to know, for every distinct combination of fldCode1 and fldCode2, if there are any years missing. For example, SELECT DISTINCT fldCode1, fldCode2 FROM MyTable returns ABC1 ABC12 ABC1 ABC13 I need to know that in 2001 there was no entry for ABC1/ABC13 Thanks! Edward (Posted in error earlier to SQL Server group - at that time I was led to believe that this was a SQL Serve...

Multiply Linear Polynomials
How can I multiply two linear polynomials ax+b and cx+d using only two multiplications? I read the question somewhere, and so far I could figure out that most probabely it should be done using something like (a+b).(c+d) I appreciate any hint. In article <1167654275.629242.168200@h40g2000cwb.googlegroups.com>, "Debula" <peyman.taher@gmail.com> wrote: > How can I multiply two linear polynomials ax+b and cx+d using only two > multiplications? > I read the question somewhere, and so far I could figure out that most > probabely it should be done using something...

string vector
i am new to matlab..i have a question, how to create a row or column vector of string using for loop?? like myArray=[jwalin shah ramesh maheta] or myArray= jwalin shah ramesh mehta On 09/22/2014 11:51 AM, jwalin wrote: > i am new to matlab..i have a question, how to create a row or column > vector of string using for loop?? > like > myArray=[jwalin shah ramesh maheta] .... doc cellstr -- ...

Three column index
I wish to have my index printed in three columns. I tried the suggestion in The LaTeX Companion, 2nd Ed., page 680, using the renewenvironment{theindex} routine. It failed when I entered the routine in the preamble to my document. I then created a new book class and added the rountine there. The index was then printed in three columns, but the entries were garbled. Please help! schlenkwad@cox.net wrote: > I wish to have my index printed in three columns. I tried the > suggestion in The LaTeX Companion, 2nd Ed., page 680, using the > renewenvironment{theindex} routine. It ...

matrix converter #3
doing my m.tech project on vector controlled induction motor drives fed from three phase to three phase matrix converter. at present working on direct space vector modulation for matrix converter using matlab 7.5 version. i have problem with duty cycle calculation and the pwm signals generation compared with triangular waves with the switching frequency of .1e-3 seconds.if anybody is having matlab 7.6(2008a) please forward me a three_phase_matrix_ converter demo. it will help me to understand the indirect space vector modulation from the demo . please kindly help me . ...

IQ multiplier
Hello, I learned that when a signal is multiplied by an IQ signal, the signal can be down sampled by 2. So assume that I have a signal that samples at 100MS and I multiplied it by an IQ signal. Then I can down sample each I and Q to 50MS. How is it working? Do I need a filter before down sampling? Or can I down sample without any filtering? Any example design that show how I can do this preferably in FPGA? Best regards ma wrote: > Hello, > > I learned that when a signal is multiplied by an IQ signal, the > signal can be down sampled by 2. So assume that ...

fastest way to find the 2 first minimum value of a sparse matrix
I have a sparse matrix and I am looking for the fastest way to find the first two minimum value of the sparse matrix but exclude zero from this two minimum value regards Siavash "Siavash Shakeri" wrote in message <j18r3f$5mr$1@newscl01ah.mathworks.com>... > I have a sparse matrix and I am looking for the fastest way to find the first two minimum value of the sparse matrix but exclude zero from this two minimum value > > regards > Siavash sorry I forgot to say that I want the 2 first minimum value along each row of the sparse matrix "Siavash Shakeri&qu...

CommandLine Option in Python for filtering values from Column
---1785158075-184816965-1397071383=:35192 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable I want to write a function in Python for a tab delimited file I am dealing = with,to filter out values from a column, and put that parameter as a comman= d line option while running the script.=0A=0ASo,if the limit is 10,the filt= er out all the rows with values less than 10.=0AAlso,I want to check if the= number entered is numeric or not else display the message "its not numeric= ".=0A=0A=0ASo,the command line would be:=0A=0A=A0 =A0 python scr...

One column index
I'm trying to make a one column index. To prepare the index I use the multind package and Xindy. In my search for solutions, I tried to do the following: \begin{theindex} \onecolumn .... But then the \onecolumn started a new page and separated the index from the index title. If instead I did: \onecolumn \printindex{myindex}{myindextitle} The title is printed on the same page as the index, but the index is two column. How can I keep a one column index on the same page as the index title? Thanks in advance, Morten Lemvigh Searching a bit more for a solution, I tried to do the foll...

HELP: matrix expanding problem
Hi, The title maybe misleading, but what I really want is the following. Assume we have a 2 by 4 matrix A = [1 2 3 4 5 0 6 0] I want to construct a matrix B B = [1 2 3 4 1 2 6 4 5 2 3 4 5 2 6 4] i.e. the rows of B represent all possible combinations of nonzero elements from each column of A. Is there an efficient way of doing this? Thanks! Jianwei: <SNIP wants to go from: A = [1 2 3 4 5 0 6 0] to B = [1 2 3 4 1 2 6 4 5 2 3 4 5 2 6 4] one of the many(!) solutions: m=A; [a,b,c,d]=ndgrid(m(:,1),m(:,...

Re: Median of column
What you want is probably more complex but the following code will throw out the highest and lowest values in your set. Data test; input id $1. value; cards; A 5 B 100 C 0 D 6 G 7 H 8 Y 9 U 11 run; Proc sort; by value; run; Data smaller; set test (firstobs = 2 ) end = eof;* start with the next to the lowest value; if eof then delete;* get rid of highest value; run; Proc print; run; Nat Wooding Environmental Specialist III Dominion, Environmental Biology 4111 Castlewood Rd Richmond, VA 23234 Phone:804-271-5313, Fax: 804-271-2977 sdlenter ...

RULE and default nextval() column
Hello. I'mtrying to implement history tables using rules. I have test_table ---------- create sequence history_seq start 1; create sequence test_sequence; # source table drop table test_table; create table test_table ( i integer default nextval('test_sequence'), c character(10) ); # history table create table test_table_history ( hist integer default nextval('history_seq'), i integer, c character(10) ); # rule to save history create rule test_table_history_insert as on insert to test_table do insert into test_table_history values ( nextval('history_...