필터 지우기
필터 지우기

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

조회 수: 2 (최근 30일)
MANISH KUMAR
MANISH KUMAR 2017년 2월 27일
댓글: MANISH KUMAR 2017년 2월 27일
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!

채택된 답변

Rik
Rik 2017년 2월 27일
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,:);

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by