Info

此问题已关闭。 请重新打开它进行编辑或回答。

call array to vectors using a for loop error problem

1 次查看(过去 30 天)
Hello
I wonder if someone can help me solve this problem:
I have a cell array named Spt < 1x1438 > and in each cell there is an array (64x5). If I run
Dir=Spt{1}(:,3);
I get a vector of Dir values. I would like to repeat this for each array within Spt so that I have 1438Dir vectors (e.g. Dir1, Dir2, Dir3,...Dir1438). Unfortunately when I run a loop I get an error:
for n=1:1438;
Dir(n)=Spt{n}(:,3);
end
??? In an assignment A(I) = B, the number of elements in B and
I must be the same.
How do I overcome this error and generate the required Dir vectors?
Thanks in advance for any advice.

回答(1 个)

Andrei Bobrov
Andrei Bobrov 2011-4-20
Dir = zeros(size(Spt{1},1),length(Spt));
for n=1:length(Spt);
Dir(:,n)=Spt{n}(:,3);
end
or
Dir = cell2mat(cellfun(@(x)x(:,3),Spt,'UniformOutput' , false))
or more
S2 = cell2mat(Spt);
Dir = S2(:,3:5:length(Spt)*size(Spt{1},2));
  1 个评论
Jan
Jan 2011-4-20
Or use a cell array:
Dir = cell(size(Spt{1},1), 1);
for n=1:length(Spt); Dir{n}=Spt{n}(:,3); end

此问题已关闭。

Community Treasure Hunt

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

Start Hunting!

Translated by