0

Is it possible to replace some rows of a matrix for specific vectors (in my case, random vectors) without using a loop in MATLAB?

Given:

A = [1 2 3; 4 5 6; 7 8 9; 10 11 12] 

I want to replace A([2 3],:) with, for instance, v = rand(1,3) but I really want that each chosen row (in this case 2 and 3) of A is randomly generated.

The problem with this is that I want to generalize it to do it for every given matrix A without using a loop in MATLAB. Is it possible?

Thank you.
Rui Semeano

2 Answers 2

3

How about:

A([2 3],:) = rand(2,size(A,2)); 

You get the number of columns in A by size(A,2), and then create a random matrix with the needed size and just assign it to the A.

If you want also to choose the rows randomly, then for N rows you can write:

N = 2; randRows = randperm(size(A,1),N); A(randRows,:) = rand(N,size(A,2)) 

P.S - if you want to randomize integers, use randi.

Sign up to request clarification or add additional context in comments.

7 Comments

hank you very much for your answers! And I am so sorry that I wasn't more specific and made you waste time, but what I actually wanted was to know was if there was a way to change some rows of a matrix to a generated vector without using a loop (or repmat). I am truly sorry for the misunderstood.
@RuiSemeano there is no loop in this answer and this is exactly what it does. The first version for a predefined rows (2 and 3) and the second for random N rows. What are you missing?
The problem is that I really should have been more specific. I don't want to use a "random generator" built in funcion. I want to use my "random generator" which outputs only one vector, not a matrix. So I want every selected row of A to be replaced by a random generated vector without using a loop or repmat. But anyway I think the better solution is to give my random generator the ability to output a matrix instead of just a vector. Thank you for your patience
@RuiSemeano So in your example, rows 2 and 3 in A will be replaced by the same random vector, or you generate a different vector for each row? And also, do you randomize the rows to change (e.g. 2 and 3) or you give them as input? And why then you accepted the other answer?
I am sorry I am new to stackoverflow I thought it meant like a check mark or something... none of the answers solved my problem but it was my fault so I validated for the effort, but I didn't noticed I could only validate one... Rows 2 and 3 should be replaced each by a call to my random function so they should be replaced by a different vector each (but can be equal by chance). No, I simply have one matrix, then I choose (randomly) some rows of that matrix and I want to replace each row by a call to my function which output is a random vector (with compatible dimension, of course.
|
0

It sound like you want to generate the [2,3] randomly? I wasn't sure so I just named everything verbosely and made everything random.

MinimumInteger = 1; MaximumInteger = 12; NumberOfRows = 4; NumberOfColums = 3; A = randi([MinimumInteger, MaximumInteger], NumberOfRows, NumberOfColums); NumberOfRowsToChange = randi([0, NumberOfRows]); RowsToChange = randperm(NumberOfRows, NumberOfRowsToChange); A(RowsToChange, :) = randi([MinimumInteger, MaximumInteger], NumberOfRowsToChange, NumberOfColums); 

1 Comment

Thank you very much for your answers! And I am so sorry that I wasn't more specific and made you waste time, but what I actually wanted was to know was if there was a way to change some rows of a matrix to a generated vector without using a loop (or repmat). I am truly sorry for the misunderstood.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.