f



affecting a matrix rows as diagonal blocks in another matrix

Hi
Suppose i have a matrix A= [1 4 5
                                        8 7 3
                                        6 5 2]
I want to have a matrix B such each row of it has a row from A at the diagonal

B=[1 4 5 0 0 0 0 0 0
     0 0 0 8 7 3 0 0 0
     0 0 0 0 0 0 6 5 2]

how can i do it. I could do it with blkdiag but it supposes i input each row for the matrix, however i need something general since i need it for bigger matrices

Thanks
0
7/11/2012 9:34:12 PM
comp.soft-sys.matlab 211266 articles. 19 followers. lunamoonmoon (257) is leader. Post Follow

2 Replies
354 Views

Similar Articles

[PageSpeed] 48

"matlab " <keep_smiling2100@yahoo.fr> wrote in message <jtkrgk$js0$1@newscl01ah.mathworks.com>...
> Hi
> Suppose i have a matrix A= [1 4 5
>                                         8 7 3
>                                         6 5 2]
> I want to have a matrix B such each row of it has a row from A at the diagonal
> 
> B=[1 4 5 0 0 0 0 0 0
>      0 0 0 8 7 3 0 0 0
>      0 0 0 0 0 0 6 5 2]
> 
> how can i do it. I could do it with blkdiag but it supposes i input each row for the matrix, however i need something general since i need it for bigger matrices

% Method 1
c = num2cell(A,2)
B = blkdiag(c{:})

% Method 2
[i j] = find(A);
B = accumarray([i (i-1)*size(A,2)+j], A(:))

% Method 3 
[i j] = find(A);
B = sparse(i, (i-1)*size(A,2)+j, A)

% Bruno
0
b.luong5955 (6401)
7/11/2012 9:56:16 PM
> % Method 1
> c = num2cell(A,2)
> B = blkdiag(c{:})
> 
> % Method 2
> [i j] = find(A);
> B = accumarray([i (i-1)*size(A,2)+j], A(:))
> 
> % Method 3 
> [i j] = find(A);
> B = sparse(i, (i-1)*size(A,2)+j, A)
> 
> % Bruno

Thanks a lot, it works pretty fine :)
0
7/12/2012 6:24:49 AM
Reply:

Similar Artilces:

Copying a matrix row into another matrix
I have NaN-filled matrix into which I want to overwrite certain rows with numeric data: VarPhysio(iSite,:) = MATVarExplicative(idx(iSite),:) where VarPhysio(iSite,:) returns Columns 1 through 13 [1.0577e+06] [NaN] [NaN] [NaN] [NaN] [NaN] [NaN] [NaN] [NaN] [NaN] [NaN] [NaN] [NaN] MATVarExplicative(idx(iSite),:) returns 1.0e+06 * Columns 1 through 13 1.0577 5.3986 0.0014 0.0001 0.0000 0.0004 0.0000 0.0010 0.0006 -0.0000 -0.0000 -0.0000 -0.0000 But I get: Conversion to cell from double is not possible. What's wrong? Oops! My error. VarPhysio(iSite,:) was a cell whereas the other matrix was not. "Luc Le Blanc" wrote in message <l5ebd2$g5$1@newscl01ah.mathworks.com>... > I have NaN-filled matrix into which I want to overwrite certain rows with numeric data: > > VarPhysio(iSite,:) = MATVarExplicative(idx(iSite),:) > > where > > VarPhysio(iSite,:) returns > > Columns 1 through 13 > > [1.0577e+06] [NaN] [NaN] [NaN] [NaN] [NaN] [NaN] [NaN] [NaN] [NaN] [NaN] [NaN] [NaN] > > MATVarExplicative(idx(iSite),:) returns > > 1.0e+06 * > > Columns 1 through 13 > > 1.0577 5.3986 0.0014 0.0001 0.0000 0.0004 0.0000 0.0010 0.0006 -0.0000 -0.0000 -0.0000 -0.0000 > > But I get: Co...

remove multiple rows of matrix based the value and index of row of another matrix
I have 2 matrices with the same row, matrixA (n by k) and matrixB (n by 1) matrixA = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 matrixB = 1 2 2 1 2 now I would like to remove row of matrix A, based the value of element in matrix B For example remov...

Block diagonal matrix from a skew-symmetric matrix
Dear Group members, I'm trying to solve the following algebra problem Consider a skew-symmetric matrix of the form: B=[0,A; -At,0] where 0 stands for a null matrix and At the transpose of matrix A. Is it possible to obtain a transformation of this matrix such as the new matrix in the new coordinates would be a block diagonal matrix of the form: C=[C1,0; 0,C2] with C=Tt * B * T, being T the transformation matrix? If possible how can I obtain such a transformation matrix? Thanks, RV ...

Matching rows in one matrix to elements of another matrix
Hi, I'm having trouble with this part of my code. I'll try to simplify the problem to explain. I have the following matrices: A = [ 1 1 1; 1 1 1; 1 1 2; 1 1 2; 1 1 2; 1 2 1; 1 2 2]; B= [1; 2; 3; 4]; And I know the following Rules: If row = 1 1 1 then output = 1 If row = 1 1 2 then output = 2 And so on&#8230; So I&#8217;m looking to assign a number in B to a row in A. So my output would look like: C = [ 1; 1; 2; 2; 2; 3; 4] I&#8217;m having trouble since 1 1 1 is a row, I tried combing the row into a single number 111 but I&#8217;m having trouble making it wo...

Select a particular element from each row of a matrix based on another matrix
I have a matrix A of size 6400 X 8 and another matrix B of size 6400 X 1( consisting of elements from 1-8). I want to extract one element from each row of matrix A according to the number(1-8) specified by matrix B. Thank you On 1/28/2012 3:06 PM, vishwanath wrote: > I have a matrix A of size 6400 X 8 and another matrix B of size 6400 X > 1( consisting of elements from 1-8). I want to extract one element from > each row of matrix A according to the number(1-8) specified by matrix B. > Thank you A(sub2ind(size(A),[1:length(B)]',B)) -- ...

Matrix of matrix
Hi everbody. First of all, I apologize for my English, so please be indulgent with me if my sentences are not grammatically correct. I have some trouble with MATLAB. I have some data in txt files that I catch like that : MFS93A1 = textread('Z:/Fichiers/MFS93.A1.txt','','headerlines',1); MFS93B1 = textread('Z:/Fichiers/MFS93.B1.txt','','headerlines',1); MFM77A1 = textread('Z:/Fichiers/MFM77.A1.txt','','headerlines',1); MFM77C1 = textread('Z:/Fichiers/MFM77.C1.txt','','headerlines',1); Then, in or...

Matrix in a matrix?
Hi to all, I've encountered a problem using Matlab. As I'm not very familiar with matlab, there are functions which I have not used before. What I would like to know, if there is a way to express a matrix within a matrix. For example, R= [ r1 r2 r3 ... rn] where rn=[xn yn tn] and xn, yn, tn are column of numbers. I used the following code below to express the numbers in rn as coloumns for easy referencing. xn=r(:,1); %represents the columns in the matrix yn=r(:,2); tn=r(:,3); My objective is to be able to retrieve information in the matrix easily and compare their values. Meani...

new matrix from another matrix
Dear all, I have a matrix of integer elements. I want to form a new matrix that sets the elements with a value, e.g 3 to one, and all others to 0s. For example, Change 1 2 3 3 5 0 to 0 0 1 1 0 0 Is there an easy method? Thanks! Charles "charles2" <c.onestone@lycos.com> wrote in message news:ef10e62.-1@webx.raydaftYaTP... > Dear all, > > I have a matrix of integer elements. I want to form a new matrix > that > sets the elements with a value, e.g 3 to one, and all others to 0s. > > For example, > Change > 1 2 3 > 3 5 0 > > to >...

Empty matrix and Diagonal matrix
Hi everyone I want to creat a large diagonal matrix.The main diagonal elements of the diagonal matrix is a 3x3 matrix. For example I have a matrix A(3x3),now I should obtain the diagnoal matrix B(300x300),can anybody tell me how to do it? Deeply appreciate your help! Thank you in advance! Nandy Erik wrote: > > > Hi everyone > I want to creat a large diagonal matrix.The main diagonal > elements > of the diagonal matrix is a 3x3 matrix. > For example I have a matrix A(3x3),now I should obtain the diagnoal > matrix B(300x300), >Others I want to know how to create an...

Matrix-to-Matrix Row Matching
Okay, this is what I am trying to do. Let's assume a matrix Z = 1 2 3 4 5 6 7 8 9 4 2 6 5 1 3 7 And another matrix Y= 1 2 4 6 7 8 5 1 3 Is there a way I can vectorize to check if each row in Y is a subset of some row in Z where 1 will be true and 0 false? For example: Given Z and Y above it. should produce a column vector 1 1 1 because all the rows in Y are subsets of some row in Z. If Y were to be say Y= 2 0 6 ...

How do I create a diagonal matrix with block matricies on the diagonal?
Hello all, I need to create a square matrix that has square block matricies along the diagonal and zeros everywhere else. That is to say, I need to create a block diagonal matrix where each submatrix is different. The overall matrix needs to have the form: M = - - | sm1 0 0 | | 0 sm2 0 | | 0 0 sm3 | - - Any advice would be greatly appreciated. Either post a reply or email me directly(phatphreak74@yahoo.ca). thanks in advance, the phreak phat phreak wrote: > Hello all, > I need to create a square matrix that has square block matricies > along the diagonal and zeros everywhere el...

Apply mask matrix to another matrix
Hi all, I just wondering how I could apply a mask matrix A(180x360) with positive and negative values (ocean/land) to another matrix with same dimension B(180x360). Where 180 is latitude and 360 longitude. Actually I want to create at least 180 arrays (1x2) (one per latitude) where appears the addition for all the positive values (land) and negative (ocean) corresponding to the matrix B. Thanks in advance. Cheers Toni ...

Arranging matrix with values of another matrix
Hello, I need to copy certain lines (for example lines 4,10,23,35,...list goes on) from matrix A into another matrix B, so that B(1,:)=A(4,:), B(2,:)=A(10,:),....... I have tried it with two for loops but there is something wrong, the values get overwritten. Maybe one of you knows the solution. I would appreciate your help! Thank you very much! "Andy M" <shshshsh@sjsjsjsjs.com> wrote in message <g95vp2 $8k8$1@fred.mathworks.com>... > Hello, > > I need to copy certain lines (for example lines > 4,10,23,35,...list goes on) from matrix A into another > matr...

All row-combinations of a matrix in a new matrix
Hello I have got a question regarding all the combinations of matrix-rows. I currently have a matrix with the following structure: 1 2 1 3 1 4 2 3 2 4 3 4 Now I want to get all the possible combinations of these "pairs" without using a number twice in the same row: 1 2 3 4 1 3 2 4 1 4 2 3 And it must be possible to make it with n-"doublecolumns". Which means, when my pair-matrix goes for example until "5 6", i want to create the matrix with 3 of these doublecolumns: 1 2 3 4 5 6 1 2 3 5 4 6 1 2 3 6 3 4 1 3 ..... I hope you understand w...

Fast Matrix*Diagonal*Matrix operation
Hi there, I have a code that computes Q'*D(t)*Q many times. Here Q is a fixed mxn matrix with m < n, while the diagonal matrix D depends on the iteration t. Because I have so many iterations that the operation slows done the entire process. I am wondering if there is any way to obtain Q'*D(t)*Q as fast as possible. Maybe from the result of Q'*Q or some kind of decomposition of Q'*Q? Thanks in advance! best, Gongguo "Gongguo Tang" wrote in message <ii7d79$lto$1@fred.mathworks.com>... > Hi there, > > I have a code that compute...

Creating a J*J Diagonal Matrix with Jth Diagonal element is also a matrix
Hello, I am a new beginner of Matlab and I want to create a Diagnoal Matrix with the main diagnoal as (A1,A2,A3,....,Aj) where Aj is also a matrix which is defined as (a11,a12,a13;a21,a22,a23;a31,a32,a33). How can I define Aj as a variable in the m file then create the diagnoal matrix through Aj? Could someone help on this please? Many Thanks in Advance! "Jie " <jz286@cam.ac.uk> wrote in message <h6selu$9ng$1@fred.mathworks.com>... > Hello, > > I am a new beginner of Matlab and I want to create a Diagnoal Matrix with the main diagnoal as (A1,A2,A3,....,Aj) w...

padding a matrix to length of another matrix
Chaps, What is the easiest way to pad matrix 1 to the length of matrix 2? And can you control where and how the padding occurs? - eg zero or ones pad, and dispersed in the matrix or at the start and end with the original data centred? Thanks Phil Phil Carles wrote: > Chaps, What is the easiest way to pad matrix 1 to the length of > matrix 2? And can you control where and how the padding occurs? - eg > zero or ones pad, and dispersed in the matrix or at the start and end > with the original data centred? help padarray -Herbert ...

block diagonal matrix
Hi all, Does anybody know what is the most efficient way to cobstruct a diagonal matrix from a vector of entries? For example I want to create: 1 0 0 0 2 0 0 0 3 from: a=1:3; "blkdiag" does not work here since I need to enter the vector elements one by one which I can't since "a" can be very large. Thanks, Elnaz On 9/5/2013 3:16 PM, Elnaz wrote: > Hi all, > > Does anybody know what is the most efficient way to cobstruct a diagonal ?matrix from a vector of entries? > For example I want to create: > 1 0 0 > 0 2 0 > 0 0 3 > from: a=1:3; > "blkdiag" does not work here since I need to enter the vector elements one >by one which I can't since "a" can be very large. > > Thanks, > Elnaz > what is wrong with diag? EDU>> a=1:3; EDU>> diag(a) 1 0 0 0 2 0 0 0 3 ...

data extraction from matrix to use in another matrix
I'm writing a code for finite element analysis and I don't know how to read a value from a matrix (A) and use it as a pointer to direct me to values in another matrix. Here is a sample of the problem. disp('Number of elements?') nelem=input(''); connectivity=[1,1,2;2,1,3;3,2,3] disp ('Elem # Node(a) Node(b)') disp(connectivity) coordinates=[1,0,0;2,2,0;3,1,2] disp ('Node # Xcoord Ycoord') disp(coordinates) disp('press enter to continue') pause for i= 1:nelem nodeax(i)=sscanf(connect(i,2)); nodeaj(i)=sscanf(connect(i,2)); end I need to read element's 1 node(a) which in this case is 1, and use that 1 to direct me to the node 1, extract the x and y coordinate from node 1, and create a matrix. Thank you in advance for the help! On 10/19/2013 11:03 AM, walter Mulvany wrote: > I'm writing a code for finite element analysis and I don't know how to > read a value from a matrix (A) and use it as a pointer to direct me to > values in another matrix. > Here is a sample of the problem. .... > connectivity=[1,1,2;2,1,3;3,2,3] .... > coordinates=[1,0,0;2,2,0;3,1,2] .... > I need to read element's 1 node(a) which in this case is 1, and use that > 1 to direct me to the node 1, extract the x and y coordinate from node > 1, and create a matrix. .... Well, it's not clear which you're after, but it's simple enough to find a locati...

replace values in a matrix using another matrix
I have two matrices with the same dimensions (saying A and B, 10x10). A is only zeros A=zeros(10); B has integer values up to 3 B=randi(3,10,10); What I'd like to do is to replace the zeros in A with values from B, only when B=3. I tried with: A(B==3)=B; but it gave me the error: ??? In an assignment A(I) = B, the number of elements in B and I must be the same. I know I could do A(B==3)=3; but this is just a simplification of my problem. I think it's quite simple, but I'm stocked... Thanks in advance to anybody who will try to answer. On 3/25/2014 9:29 AM...

Acess a matrix with the coordinates given in another matrix
Hi, I have a problem accessing to a matrix, the coordinates to access are in a different matrix. I simplify with an example. I have the coordinates in A, for example A=[1 1; 2,4; 3 6] and a matrix G size 6x6 Without a for, (optimize code) how can I access to G(1,1), G(2,4) and G(3,6). I have tried with G(A) but it doesn't work. Thanks On 5/15/2011 2:13 AM, Javier wrote: > Hi, I have a problem accessing to a matrix, the coordinates to access are in a >different matrix. I simplify with an example. > I have the coordinates in A, for example A=[1 1; 2,4; 3 6] and a matrix G size 6x6 > > Without a for, (optimize code) how can I access to G(1,1), G(2,4) and G(3,6). >I have tried with G(A) but it doesn't work. > > Thanks try G(sub2ind(size(G),A(:,1),A(:,2))) --Nasser "Nasser M. Abbasi" <nma@12000.org> wrote in message <iqo6sa$m63$1@speranza.aioe.org>... > On 5/15/2011 2:13 AM, Javier wrote: > > Hi, I have a problem accessing to a matrix, the coordinates to access are in a > >different matrix. I simplify with an example. > > I have the coordinates in A, for example A=[1 1; 2,4; 3 6] and a matrix G size 6x6 > > > > Without a for, (optimize code) how can I access to G(1,1), G(2,4) and G(3,6). > >I have tried with G(A) but it doesn't work. > > > > Thanks > > try > > G(sub2ind(size(G),A(:,1),A(:,2))) > > --Nasser Thank you very much, it works! ...

mantain rows on matrix(ismember(matrix,vector))
Hi ! I need to recover elements of a matrix that are in a vector. for now i do a = 'a' 'a' 'b' 'b' 'c' 'd' 'f' 'a' 'b' >> b={'a' 'b' 'h'} b = 'a' 'b' 'h' >> ismember(a,b) ans = 1 1 1 1 0 0 0 1 1 >> a(ismember(a,b)) ans = 'a' 'b' 'a' 'a' 'b' 'b' but what i want is to keep the rows structure. The output i want is 'a' 'a' 'b' 'b' [] [] 'a' 'b' [] where [] are empty elements it will be ok even save each single row in a cell array like this arrcell={{'a' 'a' 'b'} {'b'} {'a' 'b'}} thx Hi Fagota, How about the following? c = a; M = ~ismember(a,b); c(M) = cell(sum(M(:)),1) Best. Another solution: c = a; c(~ismember(a,b)) = {[]} ...

Inserting a row matrix into a 3x3 matrix with using a FOR loop
Hi, Suppose I've got a Matrix A: A(:,:,1) = [1 2 3; 4 5 6; 7 8 9] A(:,:,2) = [10 11 12; 13 14 15; 16 17 18] A(:,:,3) = [19 20 21; 22 23 24; 25 26 27] I've also got a row matrix, B: B = [20 30 40] Now I want to replace ALL the second rows of matrix A with matrix B, i.e. I want to get the following: A(:,:,1) = [1 2 3; 20 30 40; 7 8 9] A(:,:,2) = [10 11 12; 20 30 40; 16 17 18] A(:,:,3) = [19 20 21; 20 30 40; 25 26 27] I can of course do this with a simple For loop, but I was wondering if there are any other efficient techniques of doing this. I tried the following: A(2,:,:) = B B...

insert a matrix into matrix
hello, new to all this so excuse my ignorance lets say i have x = 1 2 3 4 which i convert to column vector x= 1 2 3 4 now i have a, created from the lengh of x in this case 4, and any value, lets say 4, so a= 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 now i want to insert x into a 4 times, so that a= 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 but i want to do this without a loop i can replace it with numbers using a(,:i)=x where i = 1 to 4 in this case, but i have to use a loop for that, did i explain that ok, can anyone help? cheeseboy On Fri, 19 Nov 2004 20:47:25 -0500, cheeseboy wrote: > hello...

Web resources about - affecting a matrix rows as diagonal blocks in another matrix - comp.soft-sys.matlab

Psychological and sociological issues affecting space travel - Wikipedia, the free encyclopedia
... and physiological issues involved with such a mission that remain to be worked out, there are also a number of behavioral issues affecting the ...

Facebook battling bug affecting link posts
Many page admins have been struggling with link posts lately, unable to upload an image to go along with the post. The image that goes along ...

Elder Christofferson Provides Context on Handbook Changes Affecting Same-Sex Marriages - YouTube
Elder D. Todd Christofferson of the Quorum of the Twelve Apostles of The Church of Jesus Christ of Latter-day Saints reaffirmed the Church’s ...

Your say, SA: The Sunday Mail’s 2015 survey on the big issues affecting our state - AdelaideNow Search ...
THIS is your chance to have a say on the big issues affecting South Australia.

Coeliac disease: the illness affecting thousands of people yet to be diagnosed
Liz Judson was so busy when it started, she didn't think to question it.

Is the way you stand affecting your relationship's stability?
If you're not feeling grounded in your relationship, you might need to stand on your own two feet.

Queensland weather: Storms affecting the state’s southeast - The Courier-Mail Search Search
WILD storms soaked parts of southeast Queensland for more than five hours last night, dumping up to 161mm of rain near Toowoomba.

Queensland - The State We're In live interactive forum to cover the issues affecting you
QUEENSLANDERS will get to quiz state government ministers directly on the big issues affecting their lives and have input on how to make this ...

Hockey cannot twiddle the tax levers for some without affecting the rest
Tax reform cannot be achieved by twiddling a lever and generating a benefit for some, the entire system is in need of an overhaul.

Australian Liberty Alliance: 'halal meat certification is affecting all of us'
A WA candidate for the newly-formed Australian Liberty Alliance says the halal certification of meat is something that affects all Australians ...

Resources last updated: 3/24/2016 8:04:15 PM