필터 지우기
필터 지우기

Matrix with repetitions of a vector

조회 수: 1 (최근 30일)
Ana Maria Alzate
Ana Maria Alzate 2019년 5월 28일
답변: Jan 2019년 5월 28일
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

답변 (2개)

dpb
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
Ana Maria Alzate
Ana Maria Alzate 2019년 5월 28일
편집: Image Analyst 2019년 5월 28일
A = repmat(M,6,4)
With this function I was getting repetition of the vector, and I need it to be sorted differently in each row.
A(i,:) = A(randperm(length(A)))
I found this function and it is working, except there are the other conditions, like rows cannot start with the same number, etc.
Image Analyst
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
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.

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by