1

I'm trying to get rid of the FOR loop and vectorize this if possible. The numerical data in the variable data1 will not be sequential data / numbers but random numerical data.

ii=0;
data1=[1,2,3]; %test data will be random data this will not be sequential numbers 
array_joined=[];
for ii = 0:2
  ii+1;
  array_joined=[array_joined; data1(:),repmat(ii,[1,length(data1)])(:)]
endfor

Result:

1   0
2   0
3   0
1   1
2   1
3   1
1   2
2   2
3   2

I'm using Octave 4.4 which is similar to Matlab.

Rick T
  • 3,349
  • 10
  • 54
  • 119
  • Probably a duplicate: https://stackoverflow.com/q/21895335/2732801 (Did not verify the solutions in octave) – Daniel Mar 25 '20 at 21:10

1 Answers1

3

repmat can be used on both data1 and iteration variable ii as follows:

data1 = [1,2,3];    ii = 0:2;  %inputs
array_joined = [repmat(data1.',numel(ii),1) repmat(ii,numel(data1),1)(:)];
Sardar Usama
  • 19,536
  • 9
  • 36
  • 58