Effacer les filtres
Effacer les filtres

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

3 vues (au cours des 30 derniers jours)
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 commentaires
Walter Roberson
Walter Roberson le 7 Nov 2023
On your system please give the command
whos -file CookCountyFoodIsecurity.mat
and show us the output.
Voss
Voss le 8 Nov 2023
Modifié(e) : Voss le 8 Nov 2023
@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
Name Size Bytes Class Attributes lowIncomeFoodDesert 1x2 4 char population 1x1 2 char povertyRate 1x2 4 char withoutCars 1x2 4 char
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')
cc1 = 1
cc2 = -1

Connectez-vous pour commenter.

Réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by