Double for loop with problems

조회 수: 10 (최근 30일)
Michela Longhi
Michela Longhi 2017년 12월 13일
댓글: Michela Longhi 2017년 12월 13일
I have a column vector temp of size 221 and another column vector x of size 2819. I want to create a new column vector from temp , of size 2819, inserting 12 zeros each number. Such as:
temp=
22
23
25
...
new_matrix=
22
0
0
0
0
0
0
0
0
0
0
0
0
23
0
0
0
0
0
0
0
0
0
0
....
But when I use a double for loop, an error appear: Subscripted assignment dimension mismatch. Error in vertices_automatic (line 109) new_matrix (j) = [temp(1:k(i),:); b; temp(k(i)+1:end,:)]
The double loop is:
for i=1:size_temp
for j=1:size_x
k (i)= 0+i; %row position, can be 1,2 or 3 ....
new_matrix (j) = [temp(1:k(i),:); b; temp(k(i)+1:end,:)]
end
end
Thanks
Michela
  댓글 수: 1
Torsten
Torsten 2017년 12월 13일
Then "temp" should be of size 13*220+1 = 2861, shouldn't it ?
Best wishes
Torsten.

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

채택된 답변

Rik
Rik 2017년 12월 13일
Just do it without a loop:
temp=[22 23 25];
new_matrix=temp(:);
new_matrix(1,13)=0;%extend with zeros
new_matrix=new_matrix';
new_matrix=new_matrix(:);
(or you could just generate a matrix/vector with zeros and put in temp with indexing)
  댓글 수: 1
Michela Longhi
Michela Longhi 2017년 12월 13일
Thanks, it's a good solution !

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by