MATLAB Answers

Find Which Row An Element Is Located At.

4 ビュー (過去 30 日間)
Joshua Blackburn
Joshua Blackburn 2020 年 4 月 9 日
編集済み: James Browne 2020 年 4 月 9 日
DataFileName = "SunSpotData.mat"; %SunSpotData.mat is a 73839x8 string array
load(DataFileName);
[R,C] = size(DailySunspots);
JanDec = DailySunspots(Jan2019:Dec2019,5); %JanDec equals the values in column 5 from rows 73415 to 73779
hold on
b = bar(str2num(char(JanDec))); %Graph
L = legend("Jan-December 2019");
hold off
title('Daily Sun Spot Activity 2019') %Cosmetics
xlabel('Day of Month');
ylabel('Number Daily Sun Spots');
From = Jan2019; %Attempt to write a program that allows you to enter a time period
To = Dec2019; %and it will find the highest number of sunspot activity and what day it is
FromTo = (From:To);
HighestNumberOfSunspots = DailySunspots(FromTo);
HighestNumberOfSunspots = max(str2num(char(DailySunspots(FromTo,5))))
Jan2019 = R-424
Dec2019 = R-60
if HighestNumberOfSunspots > 0
disp("The maximum for 2019 occured <month> <day> <year>"); %I want my output to be as such: "The maximum for 2019 occurred <month> <day> <year>.
end %Month is located in column 2, Day is located in column 3, and Year is located in column 1
In this code I'm creating a bar graph representing sunspot activity where the information is in a 73839x8 string array.
Contents:
Column 1-3 Gregorian calendar date
Column 4 Date in fraction of year
Column 5 Daily total sunspot number, A value of -1 indicates that no number is available for that day (missing value)
Column 6 Daily standard deviation of the input sunspot numbers from individual stations
Column 7: Number of observations used to compute the daily value.
Column 8: Definitive/provisional indicator. A blank indicates that the value is definitive. A '*' symbol indicates that the value is still provisional and is subject to a possible revision (Usually the last 3 to 6 months)
Programmatically Find the maximum number of sunspots for 2019
Programmatically find and print the month day and year the maximum number of sunspots occurred as
The maximum for 2019 occurred 03 21 2019
I'm not experienced in using MATLAB so my code is incredibly janky.
I figured out how to programatically find the number of sunspots from 2019, but I am having trouble recalling the row at which the highest number of sunspots resides.

  2 件のコメント

Andreas
Andreas 2020 年 4 月 9 日
Hello Joshua,
MATLAB's function max() along with the maximum value of the provided array, it also returns the index into the operating dimension, see the link below,
Therefore, I would recommend in your case to use the following command,
[HighestNumberOfSunspots, idx] = max(str2num(char(DailySunspots(FromTo,5))));
this gives you also the index idx where the maximum numer of sunspots from 2019 occurs. This index is local to the array str2num(char(DailySunspots(FromTo,5))), that is, if you create a variable,
numSunspotsJan2Dec = str2num(char(DailySunspots(FromTo,5)));
then idx defined above will be a valid index for all elements within array numSunspotsJan2Dec.
I hope this information helps.
Kind Regards,
Andreas
Joshua Blackburn
Joshua Blackburn 2020 年 4 月 9 日
Thank you very much, this method helped and I managed to get on the correct path.
-Joshua

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

回答 (1 件)

James Browne
James Browne 2020 年 4 月 9 日
Hello,
I believe I have developed a solution for you by modifying your original code. I moved the entry of the search range values to the top of the code so that it is easier to find. Hopefully this at least points you in the right direction =)
close all
clear
%Enter the starting year and month for the search range
FromYear = 2019;
FromMonth = 1;
%Enter the ending year and month for the search range
ToYear = 2019;
ToMonth = 12;
DataFileName = "SunSpotData.mat"; %SunSpotData.mat is a 73839x8 string array
load(DataFileName);
[R,C] =size(DailySunspots);
JanDec = DailySunspots(R-424:R-60,5); %JanDec equals the values in column 5 from rows 73415 to 73779
hold on
b = bar(str2num(char(JanDec))); %Graph
L = legend("Jan-December 2019");
hold off
title('Daily Sun Spot Activity 2019') %Cosmetics
xlabel('Day of Month');
ylabel('Number Daily Sun Spots');
%Initialize the start row at 0. The start row will be assigned when the
%start year and month are first found in a top to bottom search of the data
%rows
StartRow = 0;
%Find the row indices of the start and end dates in DailySunspots data set
for i = 1:R
%Convert current row month and year values to numbers to eliminate any
%problems with leading zeros (there are some in the months in 2019 and
%beyond), years converted for consistency in search range data types.
%This search order ensures that the first matching entry found is the
%first day of the month
CurrentYearNumber = str2double(DailySunspots(i,1));
CurrentMonthNumber = str2double(DailySunspots(i,2));
if isequal(FromYear,CurrentYearNumber) && isequal(FromMonth,CurrentMonthNumber)
StartRow = i;
break
end
end
%Initialize the EndRow at 0. The EndRow will be assigned when the
%end year and month are first found in a bottom to top search of the data
%rows. This search order ensures that the first matching entry found is the
%last day of the month
EndRow = 0;
%Find the row indices of the start and end dates in DailySunspots data set
for i = R: -1 :1
%Convert current row month and year values to numbers to eliminate any
%problems with leading zeros (there are some in the months in 2019 and
%beyond), years converted for consistency in search range data types
CurrentYearNumber = str2double(DailySunspots(i,1));
CurrentMonthNumber = str2double(DailySunspots(i,2));
if isequal(ToYear,CurrentYearNumber) && isequal(ToMonth,CurrentMonthNumber)
EndRow = i;
break
end
end
%Convert the sun spot data in the search range to numeric values in order
%to find the maximum
SunSpotNums = str2double(DailySunspots(StartRow:EndRow,5));
%Find the row index within the search range numeric vector, where the
%maximum number of sunspots occured
[MaxVal, MaxValIdx] = max(SunSpotNums);
%Offset the max value index by the value of the start of the search range
%index in order to locate the row of the maximum sunspot occurence in the
%original data set and correct by -1 to account fot the zero indexing in
%the original data (MATLAB vectors start at 1)
MaxSpotsIdx = MaxValIdx + StartRow -1;
%Map month numbers to month abbreviations
MonthAbbr = {'Jan'
'Feb'
'Mar'
'Apr'
'May'
'June'
'July'
'Aug'
'Sept'
'Oct'
'Nov'
'Dec'};
%Create vector of month numbers
MonthNumbers = 1:12;
%Map month numbers to month abbreviations
MonthLookup = containers.Map(MonthNumbers,MonthAbbr);
%Retrieve the year string of the row of the maximum sunspots from the data,
%within the search range
MaxSpotsYear = DailySunspots(MaxSpotsIdx,1);
%Retrieve the month number sting of the row of the maximum number of
%sunspots from the data, within the search range, convert it to a numeric
%value and use the numeric value to map it to an abbreviation
MaxSpotsMonthNum = str2double( DailySunspots(MaxSpotsIdx,2) );
MaxSpotsMonth = MonthLookup(MaxSpotsMonthNum);
%Retrieve the day string of the row of the maxumum sunspots from the data,
%within the search range
MaxSpotsDay = DailySunspots(MaxSpotsIdx,3);
%If valid start and end search indices were found, print the results of the
%search, otherwise declare an invalid search range
if (StartRow > 0) && (EndRow > 0)
fprintf('The maximum number of sunspots occurred on %s %s %s \n', MaxSpotsMonth, MaxSpotsDay, MaxSpotsYear)
else
fprintf('An invalid search range was entered')
end
Cheers

  2 件のコメント

Joshua Blackburn
Joshua Blackburn 2020 年 4 月 9 日
This produces an error on line 62:
MaxSpotsMonth = MonthLookup(MaxSpotsMonthNum);
Error using containers.Map/subsref
Specified key type does not match the type expected for this container.
I attempted looking this up and a bug report popped up stating that later updates (R2018b and R2018a Update 1) even though I have R2019a, yet I'm experiencing the issue.
Thank you for your help thus far.
James Browne
James Browne 2020 年 4 月 9 日
Hmm that must mean that the value stored in MaxSpotsMonthNum is not in the vector MonthNumbers, which is odd because it worked fine when I ran it before I pasted the code into the comment window...and works fine when I copy it from the comment window to a new script and run it. I am running MATLAB R2018b...
One thing I would do is to use the debug tool to pause the script on line 61 and see what value is stored in MaxSpotsMonthNum. I suspect that there may be something wonky going on. Are you sure you are running the script from within a directory where the SunSpotData.mat file is stored?

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

Community Treasure Hunt

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

Start Hunting!

Translated by