it shows me Check plots - "low income" - top plot, xdata Variable x must be of size [1 866]. It is currently of size [0 0]. Check where the variable is assigned a value
1 次查看(过去 30 天)
显示 更早的评论
function [corrcoef1, corrcoef2] = analyze_it(opt)
load('CookCountyFoodInsecurity.mat');
% Divide povertyRate by 100
povertyRate = povertyRate / 100;
% Assign the appropriate food desert variable based on opt value
if strcmp(opt, 'low income')
foodDesertVariable = lowIncomeFoodDesert;
elseif strcmp(opt, 'black or african american')
foodDesertVariable = blackFoodDesert;
elseif strcmp(opt, 'white')
foodDesertVariable = whiteFoodDesert;
elseif strcmp(opt, 'asian')
foodDesertVariable = asianFoodDesert;
elseif strcmp(opt, 'hispanic')
foodDesertVariable = hispanicFoodDesert;
else
error('Invalid opt value. Use one of: low income, black or african american, white, asian, hispanic');
end
% Calculate the percentage of population >0.5 miles away from a supermarket and the chosen demographic
percentage_foodDesert = (foodDesertVariable ./ population) * 100;
% Calculate the percentage of population without access to a vehicle
percentage_withoutCars = (withoutCars ./ population) * 100;
% Plotting
figure;
% Subplot 1
subplot(2,1,1);
scatter(povertyRate, percentage_foodDesert);
xlabel('Poverty Rate');
ylabel(['Percentage ' opt ' Food Desert']);
title('Correlation Coefficient: To be Calculated');
% Subplot 2
subplot(2,1,2);
scatter(percentage_foodDesert, percentage_withoutCars);
xlabel(['Percentage ' opt ' Food Desert']);
ylabel('Percentage Without Cars');
title('Correlation Coefficient: To be Calculated');
% Calculate correlation coefficients
corrcoef1 = corrcoef_mine(povertyRate, percentage_foodDesert);
corrcoef2 = corrcoef_mine(percentage_foodDesert, percentage_withoutCars);
% Add correlation coefficients to titles
title(['Correlation Coefficient: ' num2str(corrcoef1)], 'Parent', subplot(2,1,1));
title(['Correlation Coefficient: ' num2str(corrcoef2)], 'Parent', subplot(2,1,2));
end
function [corr_coef_xy] = corrcoef_mine(x, y)
n = length(x);
mean_x = sum(x) / n;
mean_y = sum(y) / n;
numerator = 0;
denominator_x = 0;
denominator_y = 0;
for i = 1:n
numerator = numerator + (x(i) - mean_x) * (y(i) - mean_y);
denominator_x = denominator_x + (x(i) - mean_x)^2;
denominator_y = denominator_y + (y(i) - mean_y)^2;
end
corr_coef_xy = numerator / (sqrt(denominator_x) * sqrt(denominator_y));
end
6 个评论
Walter Roberson
2023-11-7
On your system please give the command
whos -file CookCountyFoodIsecurity.mat
and show us the output.
Voss
2023-11-8
编辑:Voss
2023-11-8
@ronit: We need the same mat file you are using, in order to run the code with the same data you have. Please upload the mat file or direct us to the webpage where you got it.
Or we at least need to know what kind of variables (class and size) the mat file contains - this is what the following command tells us:
whos -file CookCountyFoodInsecurity.mat
As an illustration of why the data matters, here I'll run the code as-is using a mat file I made up (contents shown above). The code runs without error with this mat file.
[cc1,cc2] = analyze_it('low income')
回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!