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!