필터 지우기
필터 지우기

problem saving array data into cell in every for loop

조회 수: 3 (최근 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 CenterFile Exchange에서 Subplots에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by