# How to select the row of an element in a vector and put it in the same row for a different matrix?

1 view (last 30 days)

Show older comments

##### 0 Comments

### Accepted Answer

Kirby Fears
on 16 Feb 2016

Edited: Kirby Fears
on 16 Feb 2016

Here's an example of creating an index and using it appropriately:

A = [1;2;3];

B = NaN(3,5);

% find where A is equal to 1

idx1 = (A==1);

% Set B's column 1 to be the sum(A) in the same row where A is 1

B(idx1,1) = sum(A);

Hope this helps.

### More Answers (1)

dpb
on 16 Feb 2016

Well, much is undefined but the indexing is pretty simple...

B(A==1,icol)=sum(A);

Now, you'll have to iterate or otherwise set the column index and regenerate A and it's presumed there will only be a single value of '1' in A (else't you'll get more than one value set in B or none if didn't happen to have any). Also A must be a vector of length<=size(B,1) or you'll also be changing the size of the B array.

##### 0 Comments

### See Also

### Community Treasure Hunt

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

Start Hunting!