Logical Vectors to make find max, mean, and min
Show older comments
Hello,
I need to manipulate the Temp array (via logical vectors) to make vectors of max, mean, and min temperatures on each day of the data set. However, I keep getting the error message: Improper assignment with rectangular empty matrix. Error in (line 54) temp_max(i) = max(New_Temp(lv2)); Any help is greatly appreciated! My code is below.
clear
clc
%Number of lines of data
N = 2902;
%Number of lines before data values start
hdr = 22;
%Adjust to read past 500 limit
set(0,'Recursionlimit',N);
Time = [ ]; Temp = [ ]; Pres = [ ]; Lite = [ ];
for n = 1:14
if 1i <= 9
filename = ['Weather_Data_13-10-0' num2str(1i) '_1042.lvm'];
else
filename = ['Weather_Data_13-10-' num2str(1i) '_1042.lvm'];
end
[a, b, c, d] = textread('data.txt', '%f%f%f%f', N,'headerlines',hdr);
end
Time = [Time; a];
Temp = [Temp; b];
Pres = [Pres; c];
Lite = [Lite; d];
%Normalize Time from hours to days
Time = (Time*3600)/86400;
%Normalize time on first day to first data point (= zero)
Time = Time - Time(1);
shift = ((10 + (42/60) + (13.4/3600)))*3600/86400;
Time = Time + shift;
%Save Variables to new .mat file
save('WeatherData_Oct13.mat', 'Time', 'Temp', 'Pres', 'Lite')
clear
%Part 2:
load('WeatherData_Oct13.mat')
%Manipulate the temp array (via logical vectors, etc.) to make vectors of max,
%mean and min temperatures on each day of the data set.
lv1 = (Time > 1 & Time < 14);
New_Temp = Temp(lv1);
New_Time = Time(lv1);
temp_max = [];
temp_mean = [];
temp_min = [];
for i = 1:14
lv2 = (New_Time > i & New_Time < i+1);
temp_max(i) = max(New_Temp(lv2));
temp_mean(i) = mean(New_Temp(lv2));
temp_min(i) = min(New_Temp(lv2));
end
1 Comment
Image Analyst
on 3 Dec 2013
Edited: Image Analyst
on 3 Dec 2013
You forgot to attach your data files. No one can run your code unless you supply your data file. I've attached your original question so that when you delete it like you did your other question, it will still be here.
Answers (1)
Walter Roberson
on 3 Dec 2013
0 votes
temp_max(i) = max(New_Temp(lv2)); would error with that message if lv2 selected no elements out of New_Temp, leaving nothing to be max()'d. You cannot assign the empty element into a numeric location.
Categories
Find more on Timing and presenting 2D and 3D stimuli in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!