problem saving array data into cell in every for loop
조회 수: 4 (최근 30일)
이전 댓글 표시
Hello,
Im struggling with how to save values into a cell. My script loops 1500 times and in every loop it calculates a 6x1 array. I want to save each array value into a 6x1 cell, being each cell of length 1500.
I've tried using num2cell function but it doesnt work for me... Help me please!
The script is quite long so I have uploaded only the function that calculates what need to calc, dont know if it would be enough.
for i=1:1500
% previous operations...
%qdds is a 6x1 array calculated each loop
qdds = M\(torque-E2);
qdds= num2cell(qdds);
for indx=1:6
% I want to save qdds as a 6x1 cell, and save each array
% value into each cell for every loop
qdds{indx}(1,i)=qdds{indx}(1,i);
%s ave qds as last value qds value + Δt*qdds
qds{indx}(1,i)=qds{indx}(1,i)+deltaT*qdds{indx}(1,i);
%
qs{indx}(1,i)=qs{indx}(1,i)+deltaT*qds{indx}(1,i);
end
end
댓글 수: 0
채택된 답변
DGM
2022년 2월 6일
편집: DGM
2022년 2월 6일
I don't see why you need to complicate things with a cell array when a numeric array seems like it would work fine.
numops = 1500;
% orient qdarray whichever way you want
qdarray = zeros(6,numops);
for k = 1:numops
% do some stuff
% get a 6x1 numeric vector
qdds = rand(6,1);
% put it in the output array
qdarray(:,k) = qdds;
end
If you really want a cell array, you can.
% if you really want a cell array
qdarray = num2cell(zeros(6,numops),2);
for k = 1:numops
% do some stuff
% get a 6x1 numeric vector
qdds = rand(6,1);
% put it in the output array
for ko = 1:6
qdarray{ko}(k) = qdds(ko);
end
end
Or even simpler:
% orient qdarray whichever way you want
qdarray = zeros(6,numops);
for k = 1:numops
% do some stuff
% get a 6x1 numeric vector
qdds = rand(6,1);
% put it in the output array
qdarray(:,k) = qdds;
end
qdarray = num2cell(qdarray,2);
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!