Filter löschen
Filter löschen

Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

call array to vectors using a for loop error problem

2 Ansichten (letzte 30 Tage)
Jennifer
Jennifer am 20 Apr. 2011
Geschlossen: MATLAB Answer Bot am 20 Aug. 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.

Antworten (1)

Andrei Bobrov
Andrei Bobrov am 20 Apr. 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 Kommentar
Jan
Jan am 20 Apr. 2011
Or use a cell array:
Dir = cell(size(Spt{1},1), 1);
for n=1:length(Spt); Dir{n}=Spt{n}(:,3); end

Diese Frage ist geschlossen.

Community Treasure Hunt

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

Start Hunting!

Translated by