Create matrix from two arrays using colon

11 visualizaciones (últimos 30 días)
Daniel
Daniel el 22 de Feb. de 2014
Comentada: Walter Roberson el 17 de Nov. de 2017
I'm trying to avoid using for loops in my code as much as possible. I was advised that using vectorization would be quicker and so I though it would be good practice. The problem is that I have two column vectors and I want to create a matrix that contains the values in between the two values of each column for each row. For example if the in the first row, first column, I had the value 1 and in the first row, second column I had the value 10 I want a row vector of [1,2,3,4,5,6,7,8,9,10]. I want the code to do this for all rows.
I tried this:
indexsnips(:,1) = indexstamps_start; %89x2 double
indexsnips(:,2) = indexstamps_end; %89x2 double
indexsnips = indexsnips(:,1):indexsnips(:,2); %all starts and ends have a consistent
%difference of 60. The result should be 89x6.
The result returns only the vector from the first row, but not the others.
  2 comentarios
Image Analyst
Image Analyst el 22 de Feb. de 2014
Editada: Image Analyst el 22 de Feb. de 2014
Do they have to be integers? What if you have [1,10] in the first row for 10 values, and you have [1,6] in the second row for 6 values? Or do all rows have 10 values? You can't have different numbers of columns in a matrix.
What do yo umean that all rows have a consistent difference of 60? Do you mean like this
[1 61;
100, 160]
If so, how does your example with 10 agree with that? And if it's 60, and the new vector changes by 1, why do you have 6 columns instead of 60 columns?
Daniel
Daniel el 27 de Feb. de 2014
What I have is something like this:
blah(:,1) = [100 , 200 , 300 , 400 , 500];
blah(:,2) = [105 , 205 , 305 , 405 , 505];
I tried
m = blah(:,1):blah(:,2);
what I got was only the first row
m =
100 101 102 103 104 105
what I want is a matrix like this
m =
100 101 102 103 104 105
200 201 202 203 204 205
300 301 302 303 304 305
400 401 402 403 404 405
500 501 502 503 504 505

Iniciar sesión para comentar.

Respuestas (5)

Roger Stafford
Roger Stafford el 22 de Feb. de 2014
Assuming 1) you want integer values in your matrix, 2) the two vectors have integers, and 3) that corresponding elements in the vectors have equal differences, then do this. Let the first column vector be u and the second one v.
M = bsxfun(@plus,v,0:v(1)-u(1));
  2 comentarios
Daniel
Daniel el 27 de Feb. de 2014
Editada: Daniel el 27 de Feb. de 2014
I tried a test run on some arbitrary code:
v = [100,200,300,400,500];
u = [105,205,305,405,505];
M = bsxfun(@plus,v,0:v(1)-u(1))
answer was:
M =
Empty matrix: 5-by-0
what I want is:
m =
100 101 102 103 104 105
200 201 202 203 204 205
300 301 302 303 304 305
400 401 402 403 404 405
500 501 502 503 504 505
Roger Stafford
Roger Stafford el 27 de Feb. de 2014
What you originally stated was that "I have two column vectors" but what you have used for u and v here are two row vectors. Change them to column vectors and try it again.

Iniciar sesión para comentar.


David Sanchez
David Sanchez el 27 de Feb. de 2014
A = [100 , 200 , 300 , 400 , 500];
B = [105 , 205 , 305 , 405 , 505];
C = zeros(numel(A),numel(B)+1);
for k=1:numel(A)
C(k,:) = A(k):B(k);
end
C =
100 101 102 103 104 105
200 201 202 203 204 205
300 301 302 303 304 305
400 401 402 403 404 405
500 501 502 503 504 505

Jos (10584)
Jos (10584) el 27 de Feb. de 2014
I do not see why you have to specify the last values, since it is always the same number more than the starting values. If that is the case, you can use this
V = [100,200,300,400,500]
N = 5
% engine
C = ones(numel(V), N+1)
C(:,1) = V(:)
C = cumsum(C,2)
% or in one big unreadable but not faster step
C2 = cumsum([V(:) ones(numel(V),N)],2)
If N varies, you can use cell arrays:
V = [100,200,300,400,500]
U = [105,210,303,406, 508]
C = arrayfun(@(x) V(x):U(x),1:numel(V),'un',0)
% the cell C{k} holds the array [V(k):U(k)]
  1 comentario
Jos (10584)
Jos (10584) el 27 de Feb. de 2014
Editada: Jos (10584) el 27 de Feb. de 2014
Note that you can replace N by (U(1)-V(1)) to avoid having a variable called N ...

Iniciar sesión para comentar.


Andrei Bobrov
Andrei Bobrov el 27 de Feb. de 2014
Editada: Andrei Bobrov el 27 de Feb. de 2014
v = [100,200,300,400,500];
u = [105,205,305,405,505];
M = bsxfun(@plus,min(v(:),u(:)),0:abs(v(1)-u(1)))

Perry Liu
Perry Liu el 17 de Nov. de 2017
Editada: Perry Liu el 17 de Nov. de 2017
indexsnips(:,1)+(0:60)

Categorías

Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by