Matrix with repetitions of a vector
조회 수: 1 (최근 30일)
이전 댓글 표시
M = [1:8];
A = repmat(M,1,4)
I have these two vector. I need to create a 6x32 matrix with repetitions of the vector A, different to the original.
for i = 2:6
A(i,:) = A(randperm(length(A)))
end
I tried this loop, but I need to make sure some conditions:
First matrix, rows have to have different starting number.
Second matrix, it can happen only once that a number is followed by the same number
Third matrix, numbers 5 and 6 cannot be followed by numbers 7 and 8 respectively
댓글 수: 0
답변 (2개)
dpb
2019년 5월 28일
A=repmat(M,6,4);
is what you say literally, but I don't follow what you mean by "with repetitions of the vector A, different to the original". What is to be "different to the original" and how so?
댓글 수: 2
Image Analyst
2019년 5월 28일
Sounds like homework. Is it? Is so, we need to know so that we don't just give you the complete solution which might get you into trouble.
Do you need help setting up a "while" loop?
Jan
2019년 5월 28일
This sounds like a homework question, but you did not mention thuis explicitly. The solution is easy, but I do not want to solve your homework.
M = 1:8; % No square brackets needed, because 1:8 is a vector already
A = repmat(M, 1, 4);
ready = false;
while ~ready
Result = zeros(6, 32);
Result(1, :) = A;
for i = 2:6
Result(i,:) = A(randperm(numel(A)));
end
ready = <expression to check the wanted condition>
end
You can use this brute force approach. Fill in a test, if the wanted condition is met.
- "rows have to have different starting number": This means, that unique(A(:, 1)) must reply 6 elements.
- "it can happen only once that a number is followed by the same number": Use diff(A, 1, 2). Then sum helps to count the zeros. By the way: In which direction is "follow" defined here?
- "numbers 5 and 6 cannot be followed by numbers 7 and 8 respectively": Again "follow" is not clear. Use strfind to search for [5,7] and [6,8]. strfind works on row vectors also, but inspite of its name it accepts numerical input also.
Please try to implement this and ask a specific question on demand.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!