problem saving array data into cell in every for loop
2 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
DGM
on 6 Feb 2022
Edited: DGM
on 6 Feb 2022
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);
More Answers (0)
See Also
Categories
Find more on Subplots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!