problem saving array data into cell in every for loop

4 ビュー (過去 30 日間)
mikel lasa
mikel lasa 2022 年 2 月 6 日
コメント済み: mikel lasa 2022 年 2 月 6 日
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

採用された回答

DGM
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);
  1 件のコメント
mikel lasa
mikel lasa 2022 年 2 月 6 日
Thanks!! for me was necessary to use cells and the second option works so well!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by