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.
0 个评论
采纳的回答
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
Read more about cell arrays: https://www.mathworks.com/help/matlab/cell-arrays.html
5 个评论
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 Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!