Error: In an assignment A(I) = B, the number of elements in B and I must be the same

Not sure if anyone will beable to help me but thought I might as well ask.
This is my code I'm currently using:
%%Define Variables
g=9.81 %define gravity as 9.81
Numbertrials=90 %number of trials
FrameR = 0.001 %Frame rate was 1000
%Data in
for i=[1:Numbertrials]
if i<10
filein='Trial0';
else filein='Trial';
end
datain = csvread([filein num2str(i) '.CSV'],5,2); %read data in from the csv files
%%Find peak jump height
Num0(i)=find(datain(2790:6000,1)==0);%How many 0 readings = time in air
FT(i)=length(Num0)*FrameR; %Time of flight
PeakDisp(i)=(0*(FT/2))-(0.5*g*(FT/2)^2); %Peak Jump Height using vt-0.5*a*t^2
end
I am getting an error on the Num0(i)=...line. I know this is because find returns all row numbers where the condition is met, so I get a lot of row numbers to come out of this function. And I am trying to fit them all into a single element (Num0(i)) which it can’t do, hence the error.
So I was wodnering if anyone knew how I could fix this easily?
If any extra information is needed let me know
Thanks

 採用された回答

Sara
Sara 2014 年 5 月 20 日
If you do not need to store Num0 and have it after the for loop ends, you could just do
Num0 = find....
especially because at the next line you do:
FT(i)=length(Num0)*FrameR;
which suggest you just want to know how many 0 readings you have but not where they are in datain.

2 件のコメント

BOB
BOB 2014 年 5 月 21 日
編集済み: BOB 2014 年 5 月 21 日
I now get the same error on the line
PeakDisp(i)=(0*(FT/2))-(0.5*g*(FT/2).^2);
Any ideas? Do I need to change the FT in that line to FT(i)?
Sara
Sara 2014 年 5 月 21 日
If you don't need to know the evolution of FT with i, then yes, just replace FT(i) with FT. Or, do:
PeakDisp(i)=(0*(FT(i)/2))-(0.5*g*(FT(i)/2).^2);
Also, preallocate PeakDisp before the for loop:
PeakDisp = zeros(Numbertrials,1);
for speed.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

タグ

質問済み:

BOB
2014 年 5 月 20 日

コメント済み:

2014 年 5 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by