Please help me in generating new row vectors from a given row vector.

1 view (last 30 days)
For example, I have a row vector A
A = [ 4 2 6 1 5 ];
Whose elements are bounded by an upper bound
UB = [ 5 4 6 3 5 ]
Now from this row vector 'A', N (length (A)), new vectors are to be generated. In each vector, an element of the vector A changes the value randomly within the upper bound and rest of the elements remains same.
For example, for above vector A, in first new vector A1, first element changes randomly within bound of [0-5] and rest of the elements remains same. Similarly, other vectors are generated.
A1 = [ 3 2 6 1 5 ];
Rests of the new vectors are
A2 = [ 4 1 6 1 5 ];
A3 = [ 4 2 3 1 5 ];
A4 = [ 4 2 6 3 5 ];
A5 = [ 4 2 6 1 2 ];
I am using this code but couldn’t get the desired answer.
clc
clear all
UB = [ 5 4 6 3 5 ]
A = [ 4 2 6 1 5 ];
for i = 1:length(A)
A(i) = randi(UB(i));
end
Please help. Thanks in anticipation!

Accepted Answer

Rik
Rik on 27 Feb 2017
You need to think if you don't want the results in a matrix or a cell. Below is an adaptation that should do what you want.
UB = [ 5 4 6 3 5 ];
A = [ 4 2 6 1 5 ];
C=zeros(length(A));%create an empty 5x5 matrix
for i = 1:length(A)
C(i,:) = A;
C(i,i) = randi(UB(i));
end
%copy to separate variables:
A1=C(1,:);
A2=C(2,:);
A3=C(3,:);
A4=C(4,:);
A5=C(5,:);

More Answers (0)

Community Treasure Hunt

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

Start Hunting!