필터 지우기
필터 지우기

Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Create a vector for each element in another vector to constitute a matrix

조회 수: 1 (최근 30일)
ABDULAZIZ ALTUN
ABDULAZIZ ALTUN 2020년 6월 30일
마감: MATLAB Answer Bot 2021년 8월 20일
Consider the following vector
v=[1:5]'
I want to create for each element of v another v, such that we have the following matrix
[1 1; 1 2; 1 3; 1 4; 1 5; 2 1; 2 2; ect]
Is there a shortcut or a code trick?
I tried this and it works but I wonder if there is a better way of doing it
v=[1:5]';
v1=repmat(v,length(v),1);
v2=repmat(v',length(v),1);
M=[v1(:) v2(:)];
M is what I want.
  댓글 수: 3
KSSV
KSSV 2020년 6월 30일
Thats what you can do...use repmat/ repelem.

답변 (1개)

madhan ravi
madhan ravi 2020년 6월 30일
편집: madhan ravi 2020년 6월 30일
[repelem(v, numel(v)), repmat(v, numel(v), 1)]
%or
[V1, V2] = meshgrid(v,v)
[V1(:), V2(:)]

이 질문은 마감되었습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by