Matlab Concatenation Question Help
显示 更早的评论
Hi, so I am completing the following question for a HW:
"Write a function called year2016 that returns a row-vector of struct-s whose elements correspond to the days of a month in 2016 as specified by the input argument. If the input is not an integer between 1 and 12, the function returns the empty array. Each struct should contain three fields with these (exact) field names: “month”, “date”, and “day” (all lower case). The month field must contain a string with the name of the month (first letter capitalized). The date field must contain a scalar specifying the day of the month. The day field must contain the three-letter abbreviation of the day chosen from this list: 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'. For example, here is a call of the function followed by a command that shows the seventh element of the struct array that is returned by the function: >> m = year2016(2); >> m(7) ans = month: 'February' date: 7 day: 'Sun'"
I have got my code as below;
function out = year2016(m)
if m < 1 | m > 12
month.date.day = ''
out = struct(month.date.day)
elseif ~(floor(m)==m)
month.date.day = ''
out = struct(month.date.day)
else
VN = datenum([2016,m,1]):datenum([2016,m+1,1])-1;
DN = 1+mod(VN-3,7);
MC = {'January';'February';'March';'April';'May';'June';'July';'August';'September';'October';'November';'December'};
DC = {'Mon','Tue','Wed','Thu','Fri','Sat','Sun'};
out = struct('day',DC(DN),'date',num2cell(1:numel(VN)));
for i = 1:m(end)
[out(:).month] = deal(MC{i});
end
end
end
the following error message keeps appearing and I am unsure how to make the matrices equal. "Error using horzcat Dimensions of matrices being concatenated are not consistent.
Error in year2016 (line 14) VN = datenum([2016,m,1]):datenum([2016,m+1,1])-1;" This occurs when m = [1 1; 1 1]. Any suggestions would be appreciated, thanks.
回答(1 个)
Well, you have taken my original code and made some changes to it: some of the changes are okay, and some are not (e.g the deal in a loop). The error you are getting is because you do not check for a scalar input (below I show you how to fix this). Just for reference, my original code was (copied from my answer to this question, so this code is already in the public domain).
function out = year2016(m)
VN = datenum([2016,m,1]):datenum([2016,m+1,1])-1;
DN = 1+mod(VN-3,7);
MC = {'January';'February';'March';'April';'May';'June';'July';'August';'September';'October';'November';'December'};
DC = {'Mon','Tue','Wed','Thu','Fri','Sat','Sun'};
out = struct('day',DC(DN),'date',num2cell(1:numel(VN)));
[out(:).month] = deal(MC{m});
end
And tested:
>> m = year2016(12); m(8)
ans =
day = Thu
date = 8
month = December
>> m = year2016(1); m(1)
ans =
day = Fri
date = 1
month = January
>> m = year2016(2); m(29)
ans =
day = Mon
date = 29
month = February
>> m = year2016(4); m(1)
ans =
day = Fri
date = 1
month = April
There is a long discussion of how to test for scalar input here:
As you can see from that thread, your code is more complicated than it needs to be, because all you need to do is this:
if isscalar(??) && fix(m)==m && m>=?? && m<=??
...
else
...
end
deal in a loop:
Putting the deal operator into a loop serves no purpose: I did not use a loop in my original code, and yet my code worked... so why take code that already works and make a pointless change to it? A loop is not required because I used a comma-separated list to allocate the month name to every element of the structure all at once (that means without a loop).
Not only is the change not required, this loop simply repeats allocating the month to the entire structure 28/29/30/31 times:
for i = 1:m(end) % why use |end| if m is a scalar?
[out(:).month] = deal(MC{i});
end
So instead of performing 28/29/30/31 allocations all at once like I did, this code also repeats those allocations 28/29/30/31 times, all of which are a total waste because they are immediately replaced by the allocations on the next loop iteration. The correct way to implement this using a loop would be to use indexing:
for k = 1:numel(out)
out(k).month = MC{i};
end
类别
在 帮助中心 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!