Remove elements from matrix if length below certain number

2 次查看(过去 30 天)
Hi, I would like to remove elements from my matrix if it doesn't meet certain conditions. My code is as below but I get an error message 'Matrix index is out of range for deletion'. Can someone tell me where I've gone wrong? Thanks.
%
[data number] = size (Power)
years = unique(Wind_all(:,1);
for power_curve = 1:numbers %for each power curve
for yrs = 1:length(years) %for each year of Wind_all
% interpolate power curve to wind speed
Power_out = interp1(speed(:, power_curve), power(:, power_curve), Wind_all(find(Wind_all(:,1) == years(yrs), 2));
%%I want to remove Power_out if length(Power_out) is below 8760 i.e. less than 1 year of wind speed data
ndata (yrs, power_curve) = length(Power_out)
idx = find(ndata<8760);
Actual_gen(yrs, power_curve) = nansum(Power_out);
Actual_gen(idx) = [];
end
end

回答(1 个)

Guillaume
Guillaume 2018-6-11
This part of the code makes no sense to me:
ndata (yrs, power_curve) = length(Power_out)
idx = find(ndata<8760);
Actual_gen(yrs, power_curve) = nansum(Power_out);
Actual_gen(idx) = [];
You're filling up 2d matrices, and then try to delete single elements of a 2d matrix. Of course, unless you delete whole rows or columns of a 2d matrix, matlab will reshape the matrix into a vector since you can't have 2d matrices with holes in them. Also, if at a step of the loop an element of ndata was smaller than 8760, you'd delete the corresponding element of Actual_gen. On the next step of the loop, that small ndata value would still be present and you'd try to delete the corresponding element of Actual_gen that you've already deleted the previous step. Instead you'll be deleting the next element that is now replacing the deleted element. Do that too many times and you'll run out of elements to delete as your vector shrinks more and more.
I would suspect that the deletion needs to be done only once, after the loop.
Also note that none of the find in your code are necessary. Perhaps, this is want you want:
[data, number] = size(Power); %Matlab will warn you to use , between output values and to terminate lines with ;
years = unique(Wind_all(:,1)); %There was a missing ) on that line
ndata = zeros(numel(years), number); %Predeclare ndata
Actual_gen = zeros(numel(years), number); %Also predeclare Actual_gen
for power_curve = 1:numbers %for each power curve
for yrs = 1:numel(years) %for each year of Wind_all
% interpolate power curve to wind speed
Power_out = interp1(speed(:, power_curve), power(:, power_curve), Wind_all(Wind_all(:,1) == years(yrs), 2));
ndata(yrs, power_curve) = numel(Power_out)
Actual_gen(yrs, power_curve) = nansum(Power_out);
end
end
Actual_gen(ndata < 8760) = []; %delete all values of Actual_gen for which the corresponding ndata is less than 8760.
But of course, as said, after that deletion Actual_gen will be a vector. If it's not what you want then explain better what you're trying to do.

类别

Help CenterFile Exchange 中查找有关 Robustness and Worst-Case Analysis 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by