필터 지우기
필터 지우기

creating matrix of unique integeres

조회 수: 1 (최근 30일)
Nabin SUNAM
Nabin SUNAM 2015년 2월 18일
편집: Stephen23 2015년 2월 18일
I need to create a matrix of size 2*6, with elements as the integers from 1 through 12. None of these integers can repeat. I tried the following way. Is there some other way to do it with the use of loop instead of using randperm function?
clear all
vec = zeros(6,2);
vec1 = randperm(12);
for i = 1:12
vec(i) = vec1(i);
end
disp(vec)
  댓글 수: 2
Geoff Hayes
Geoff Hayes 2015년 2월 18일
Nabin - what is wrong with using randperm? Is it because the output is not a 2x6 matrix? If test is the case, then use reshape to change the twelve element array output of randperm to a 2x6 matrix.
Nabin SUNAM
Nabin SUNAM 2015년 2월 18일
It is 2x6 matrix. I'm just curious if it can be done without any built in function.

댓글을 달려면 로그인하십시오.

답변 (1개)

Image Analyst
Image Analyst 2015년 2월 18일
You don't need to use randperm at all. Here's a way that uses loops and meets all your criteria:
k = 1;
for col = 1 : 6
for row = 1 : 2
vec(row, col) = k;
k = k + 1;
end
end
  댓글 수: 2
Nabin SUNAM
Nabin SUNAM 2015년 2월 18일
Sorry I didn't mention one important stuff in the question. Those elements can't be in order. They need to look like they were picked randomly - only 1 through 12, but in random order.
Stephen23
Stephen23 2015년 2월 18일
편집: Stephen23 2015년 2월 18일
@Nabin SUNAM: So you want exactly what randperm can do... but without randperm. Why?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by