Save data from a for loop with different dimensions?

Hello I'm trying to store the data of the next loop by vectorizing each iteration:
lambda = 2;
distri= poissrnd(lambda,1,5);
xi= distri
for x = xi
r(x)= randi([1,4],1,x)
end
But I get this error: Subscripted assignment dimension mismatch.
I think is because the different results of the loop are of different sizes so they can´t be put in a single vector, so there is another way to save my data?

 채택된 답변

Ameer Hamza
Ameer Hamza 2018년 6월 17일
편집: Ameer Hamza 2018년 6월 17일
Each iteration has a different number of elements so you will need a cell array. Also, you will need to change the indexing of for loop
lambda = 2;
distri= poissrnd(lambda,1,5);
xi= distri;
for x = 1:numel(xi)
r{x}= randi([1,4],1,xi(x));
end

댓글 수: 2

Thanks it worked perfectly, I didn't know my index was so wrong
You are welcome.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2018년 6월 17일

댓글:

2018년 6월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by