Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

call array to vectors using a for loop error problem

1 vue (au cours des 30 derniers jours)
Jennifer
Jennifer le 20 Avr 2011
Clôturé : MATLAB Answer Bot le 20 Août 2021
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.

Réponses (1)

Andrei Bobrov
Andrei Bobrov le 20 Avr 2011
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 commentaire
Jan
Jan le 20 Avr 2011
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