Create a difference loop from a cumulative matrix

8 ビュー (過去 30 日間)
Mohamed Amine Mouajib
Mohamed Amine Mouajib 2019 年 4 月 8 日
回答済み: Mohamed Amine Mouajib 2019 年 4 月 8 日
Hello
I have this data filed that I imported into a 831x1 matrix. The numbers in it are cumulative so I need to create a for loop that shows the difference between the row and the one before it so that I can have the pure added quantity in each row. If possible, I would like the first row to stay the same as it was in the imported file.
How do I do this?
  4 件のコメント
madhan ravi
madhan ravi 2019 年 4 月 8 日
Why are you sticking with a loop , is it your homework?
Mohamed Amine Mouajib
Mohamed Amine Mouajib 2019 年 4 月 8 日
Yes. Nevermind I managed to do it.

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

採用された回答

Raj
Raj 2019 年 4 月 8 日
Suppose 'A' is your 831x1 matrix. Use this:
B=diff(A);
Apure=[A(1);B];
If you are specific with "for loop" then you can use this:
Apure=zeros(831,1);
Apure(1)=A(1);
for i=2:831
Apure(i)=A(i)-A(i-1);
end
  3 件のコメント
madhan ravi
madhan ravi 2019 年 4 月 8 日
編集済み: madhan ravi 2019 年 4 月 8 日
@Mohammed: Why is there a loop in your code?, even without the loop you would get the same result. Basically you are just repeating the same calculation over and over again.
Mohamed Amine Mouajib
Mohamed Amine Mouajib 2019 年 4 月 8 日
It's supposed to be a loop. The reason I added C is because I have to use it in a calculation.

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

その他の回答 (1 件)

Mohamed Amine Mouajib
Mohamed Amine Mouajib 2019 年 4 月 8 日
Here's my final script for the whole problem
% Amine Mouajib
% Homeowork 4
% I ommited the text part of the fuel_totalizer.txt file
%in preferences, I changed the command window numerical format from short
% to long g
clc, clear all
disp('Problem 1')
A=[0;importdata('fuel_totalizer.txt')]
y=0
sec=size(A,1)
hour=sec/60
disp('Problem 2,3,&4')
for m=1:length(A)
B= diff(A) % Fuel consumption in each consecutive minute
C= diff(A/60) % Flow rate in lbs/sec for each minute
sum(diff(A)) % Total fuel consumed. Notice it's the same value as the last row of given data matrix
end
disp('Problem 5')
avg=sum(diff(A))/hour %avg fuel consumption for whole flight in lbs/hr
disp('Problem 6')
takeoffmin=1.5*mean(diff(A))% the minimum possible minute flow rate during takeoff in lbs/min
E=B(1:416)% First half of data
D=E(E>takeoffmin)
takeofftime=size(D,1)% in minutes
disp('bonus')
bns=max(C) % the maximum flow rate in lbs/sec
bns2=max(B)
Z=find(B == bns2) % which minute the maximum flow rate in lb/sec occured

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by