Create matrix from two arrays using colon

11 ビュー (過去 30 日間)
Daniel
Daniel 2014 年 2 月 22 日
コメント済み: Walter Roberson 2017 年 11 月 17 日
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 件のコメント
Image Analyst
Image Analyst 2014 年 2 月 22 日
編集済み: Image Analyst 2014 年 2 月 22 日
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 2014 年 2 月 27 日
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

サインインしてコメントする。

回答 (5 件)

Roger Stafford
Roger Stafford 2014 年 2 月 22 日
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 件のコメント
Daniel
Daniel 2014 年 2 月 27 日
編集済み: Daniel 2014 年 2 月 27 日
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 2014 年 2 月 27 日
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.

サインインしてコメントする。


David Sanchez
David Sanchez 2014 年 2 月 27 日
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) 2014 年 2 月 27 日
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 件のコメント
Jos (10584)
Jos (10584) 2014 年 2 月 27 日
編集済み: Jos (10584) 2014 年 2 月 27 日
Note that you can replace N by (U(1)-V(1)) to avoid having a variable called N ...

サインインしてコメントする。


Andrei Bobrov
Andrei Bobrov 2014 年 2 月 27 日
編集済み: Andrei Bobrov 2014 年 2 月 27 日
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 2017 年 11 月 17 日
編集済み: Perry Liu 2017 年 11 月 17 日
indexsnips(:,1)+(0:60)
  1 件のコメント
Walter Roberson
Walter Roberson 2017 年 11 月 17 日
Note: this requires R2016b or later.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by