How can I add a set of data to an array in a for loop n number of times without rewriting the previous data in the array

조회 수: 1 (최근 30일)
for i=1:1:100
while A(i)~=1
%do some data processing of A and obtain B
idx=find(B==0)
S=idx
A(idx)= 1;
end for
end while
In this program how do I store all the values of idx in S for every itereation of the loop without over writing the previous stored values of idx in S. I need all such values of idx for future use.

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 4월 16일
편집: Ameer Hamza 2020년 4월 16일
Save in a cell array.
S = cell(1,100);
for i=1:1:100
while A(i)~=1
%do some data processing of A and obtain B
idx=find(B==0)
S{i}=idx % cell array indexing
A(idx)= 1;
end % while
end % for
Access the element like this
S{1} % idx for i=1
S{2} % idx for i=2
  댓글 수: 5
Ameer Hamza
Ameer Hamza 2020년 4월 16일
Normal array in MATLAB cannot have data of different lengths. Each row and column must have an equal number of elements.

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

추가 답변 (0개)

카테고리

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