Filter löschen
Filter löschen

Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

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

1 Ansicht (letzte 30 Tage)
ABDULAZIZ ALTUN
ABDULAZIZ ALTUN am 30 Jun. 2020
Geschlossen: MATLAB Answer Bot am 20 Aug. 2021
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 Kommentare
KSSV
KSSV am 30 Jun. 2020
Thats what you can do...use repmat/ repelem.

Antworten (1)

madhan ravi
madhan ravi am 30 Jun. 2020
Bearbeitet: madhan ravi am 30 Jun. 2020
[repelem(v, numel(v)), repmat(v, numel(v), 1)]
%or
[V1, V2] = meshgrid(v,v)
[V1(:), V2(:)]

Diese Frage ist geschlossen.

Community Treasure Hunt

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

Start Hunting!

Translated by