Fill NaN matrix with vector of unequal sizes

15 次查看(过去 30 天)
Hi all,
I have the following code:
temp = {'AccNorm','GyrNorm'};
NSensors = 5;
for a = 1:length(temp)
for k = 1:NSensors
CMC_RT(k).RF.(temp{a}) = nan(10,2);
end
end
In which I just preallocate CMC_T with NaN values, because the 2 vectors I want to put in the matrix have different lengths.
However when I try to fill them up:
for a = 1:length(temp)
for k = 1:NSensors
CMC_RT(k).RF.GyrNorm = CMC_RTT.RF.(temp{a})(k,1:10)';
CMC_RT(k).RF.GyrNorm(:,2) = CMC_RTT.RF.(temp{a})(k,11:19)';
end
end
Matlab gives me the error: Unable to perform assignment because the size of the left side is 10-by-1 and the size of the right side is 9-by-1.
Which obviously makes sense, but I would think matlab would just fill the NaN matrix with the numbers and leave the NaN values for the ones where there is no substitute value. But this was apparently whishful thinking...
Any help on a idea for a solution would be much appreciated.
Thank you!

采纳的回答

Stephen23
Stephen23 2019-10-18
CMC_RT(k).RF.GyrNorm(1:10,1) = CMC_RTT.RF.(temp{a})(k,1:10);
CMC_RT(k).RF.GyrNorm(1:9,2) = CMC_RTT.RF.(temp{a})(k,11:19);
The complex conjugates are not required.
  6 个评论
CheshireM
CheshireM 2021-10-27
@Stephen This code just gives me an error
Index exceeds the number of array elements (1000)
Stephen23
Stephen23 2021-10-27
编辑:Stephen23 2021-10-27
"I should do something like this?"
One loop should be sufficient:
n = cellfun(@numel,t);
m = nan(numel(t),max(n));
for k = 1:numel(t)
m(k,1:n(k)) = t{k};
end
This code assumes that cell array t and its content are vectors.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Particle & Nuclear Physics 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by