필터 지우기
필터 지우기

Fill an array in a for loop, with change of dimension

조회 수: 13 (최근 30일)
luca
luca 2019년 7월 26일
편집: KALYAN ACHARJYA 2019년 7월 26일
Hi, I have the following code and I have to fill the array T with y every loop.
y=zeros();
t=0;
nraw=420
for kk = 1:nraw
for iii = 1:length(comb)-1 %% NON MI PIACE IL MENO 1
t=t+1;
if comb(t) == comb(t+1)
y(iii) = 1
else
y(iii) = 2
end
end
T(kk,:)=y
end
The main problem is that length(comb) change every time, and so filling T I receive the following error
Unable to perform assignment because the size of the left side is 1-by-4 and the size of the right side
is 1-by-3.
Error in TAMBURILIMITATI2507 (line 150)
T(kk,:)=y
In fact, for the first iteration length(comb) was 5, but when become 4 the for cycle failed cause I cannot fill T
Do you know how to fix this case?

채택된 답변

KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 7월 26일
편집: KALYAN ACHARJYA 2019년 7월 26일
"Fill an array in a for loop, with change of dimension"
Use cell array
Example
a={};
for i=1:...
a{i}=
end
Now a{1},a{2} can be any size within loop
Resultant may be look like
a={ array1 array2 array 3 array 4}
Here array1 array2 array 3 array 4...can have any sizes
  댓글 수: 10
luca
luca 2019년 7월 26일
is it possibile to insert a 0 where there is the fourth element missed and so enable to get a result?
KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 7월 26일
편집: KALYAN ACHARJYA 2019년 7월 26일
Yes, do it (if required) during array calculations within loop, make them all array having 4 length, lesser fill with 0.
But I have no idea, why you are intended to doing so.
Good Luck!

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by