Possible to simplify vector expression?

While writing a script...
My goal was to have a vector defined with a long list of terms that show the difference in adjacent temperatures for an already defined column of values(male_temperature).
temp_diff = [(male_temperature(1) - male_temperature(2)) (male_temperature(2) - male_temperature(3)) all the way to (male_temperature(23) - male_temperature(24))]
after much trouble and research, managed to simplify it to:
for k = 1:23;
v = genvarname('d', who);
eval([v ' = male_temperature(k) - male_temperature(k+1)']);
end
temp_diff = [d d1 d2 d3 d4 d5 d6 d7 d8 d9 d10 d11 d12 d13 d14 d15 d16 d17 d18 d19 d20 d21 d22]
clear d d1 d2 d3 d4 d5 d6 d7 d8 d9 d10 d11 d12 d13 d14 d15 d16 d17 d18 d19 d20 d21 d22
Is there a better way to simplify this or a different way of doing this. Is there any way to better simplify the vector expression (temp_diff) at all? It is inefficient having to type out all these variables.

1 件のコメント

John Mahoney
John Mahoney 2014 年 12 月 5 日
Do you want this?
temp_diff = diff(male_temperature)
Actually, since you define temp_diff as [mt(1) - mt(2), …] I think you want
temp_diff = -diff(male_temperature)

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

 採用された回答

Devin Mair
Devin Mair 2014 年 12 月 5 日

0 投票

for step = 1:23
temp_diff(step) = male_temperature(step) - male_temperature(step +1);
end
I think this would work for you, based on my understanding of the problem.

3 件のコメント

Ryan
Ryan 2014 年 12 月 5 日
The outputs for both my version look strange and different than usual.
Typically I will get an ouput that looks like this:
temp_diff =
2 3 4 5 6 7 8 9 10 11
But with both my code and yours, I get
temp_diff =
Columns 1 through 6
0.0600 0.0200 -0.0100 0 -0.1100 -0.0700
Columns 7 through 12
-0.0700 -0.0200 0.0600 -0.1600 -0.1000 -0.1000
The numbers aren't really relevant my question is about the style change. Do they behave the same, how can i convert to the form that I am used to. Is it even necessary?
John Mahoney
John Mahoney 2014 年 12 月 5 日
Ryan, It looks like both outputs are a single row of numbers. The reason the second output has "Columns 1 through 6" etc is because of the length of the array on the screen. Try
a = 2:30
and you will see the same thing.
Btw, take a look at my answer to your problem above. I think it has several advantages. You don't need to know the length of the array, and therefore you can't screw up looping over the correct number (-1). Also, for very large data sets it can be faster. More importantly, it is just good to be aware of the functions within Matlab that can perform simple tasks on entire arrays "at once" - so called "vectorized" functions. Good luck - John
Devin Mair
Devin Mair 2014 年 12 月 5 日
Using the built in function is definitely preferable. John's method should be used in this case.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

製品

質問済み:

2014 年 12 月 5 日

コメント済み:

2014 年 12 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by