필터 지우기
필터 지우기

Generate specific matrix from an array

조회 수: 3 (최근 30일)
Pierre
Pierre 2013년 10월 1일
댓글: Laurent 2013년 10월 1일
I have an array A=[-2 -1 0 1 3 5] which has 6 values and maximum value is 5 and minimum value is -2. I wanna generate a 6 by 10(=5+abs(-2)+3) matrix B. B=[1 1 1 0 0 0 0 0 0 0; 0 1 1 1 0 0 0 0 0 0; 0 0 1 1 1 0 0 0 0 0; 0 0 0 1 1 1 0 0 0 0; 0 0 0 0 0 1 1 1 0 0; 0 0 0 0 0 0 0 1 1 1 ]. We can assume the virtual number of column in matrix B is [-3 -2 -1 0 1 2 3 4 5 6]. Each row data in matrix B is from each number in array A. For example, -2 in array A means we should set (-2+1=-1), (-2) and (-2-1=-3) for 1 on the first row in matrix B, so the value should be [1 1 1 0 0 0 0 0 0 0]. -1 in array A means we should set (-1+1=0), (-1) and (-1-1=-2) for 1 on the second row in matrix B, so the value should be [0 1 1 1 0 0 0 0 0 0]. In this example, the size of array A is 1 by 6, Actuallly, it could be 1 by n. Please tell the faster way to generate matrix B. Thanks

채택된 답변

Pierre
Pierre 2013년 10월 1일
Sorry, I posted MATLAB code again. Is there any person to tell me whether there is other faster mwthod to generate matrix B? Thanks.
A=[-2 -1 0 1 3 5 ] ;
[size_r, size_c]=size(A);
max_value=max(A);
min_value=abs(min(A));
B=zeros(size_c, max_value+min_value+3);
row_num=[1:1:size_c,1:1:size_c,1:1:size_c];
col_num=[A+min_value+1,A+min_value+2,A+min_value+3];
idx=sub2ind(size(B),row_num,col_num);
B(idx)=1;
  댓글 수: 1
Laurent
Laurent 2013년 10월 1일
Did you measure the time? On my machine my code is about 5 to 6 times faster than your code. How big will your n be in the end?

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Laurent
Laurent 2013년 10월 1일
편집: Laurent 2013년 10월 1일
Below I show one way to do it. For big arrays there might be a faster way if you don't use for-loops, but for small n there is no need for this.
A=[-2 -1 0 1 3 5];
B=zeros(length(A),10);
tempA=A+3;
for ii=1:length(A)
B(ii,tempA(ii):tempA(ii)+2)=1;
end
If you want the size of B to depend on the values in A you could replace line 2 with:
B=zeros(length(A), max(A)+5);
  댓글 수: 1
Pierre
Pierre 2013년 10월 1일
if possible, is there any way to generate this matrix without for loop?

댓글을 달려면 로그인하십시오.


Pierre
Pierre 2013년 10월 1일
I found this way to generate it as following. Is there any person to tell me whether there is other faster mwthod to generate matrix B? Thanks.
A=[-2 -1 0 1 3 5 ] [size_r, size_c]=size(A); max_value=max(A); min_value=abs(min(A)) B=zeros(size_c, max_value+min_value+3); row_num=[1:1:size_c,1:1:size_c,1:1:size_c];
col_num=[A+min_value+1,A+min_value+2,A+min_value+3]; idx=sub2ind(size(B),row_num,col_num); B(idx)=1;
  댓글 수: 1
Jan
Jan 2013년 10월 1일
Please follow the "? Help" link on this page to learn how to format code in the forum. Posting not readable code is useless.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by