Problem - vectors must be the same size

2 views (last 30 days)
I have a function below that put 36 vectors on top of each other. I do this so I can plot it with another vector I3 from another function.
If I set the nr to smaller numbers up to 31 it works, but then I get the message:
Error using plot
Vectors must be the same length.
Error in PLOTTING (line 21)
plot(I3,D,'.')
Is there another way to put vectors on top of each other?
Thanks
Here is my function
function [D]=AH(nr,AAG,s)
for y=1:nr
xx=cell(length(AAG{y}),1);
for o=1:length(AAG{y})
xx{o} =[AAG{y}{o} zeros(1,s-length(AAG{y}{o}))];
end
for k1 = 1:length(AAG{y})
%
%
%
% %Histogram over all values
if k1==1
G(k1:length(xx{k1}))=xx{k1};
else
G((k1-1)*s+1:k1*s)=xx{k1};
% G=[G xx{k1}];
end
%
%
%Plotting inhabitants
% scatter(ones(1,numel(xx{k1}))*y, xx{k1},ones(1,numel(xx{k1}))*I{k1},'s','Linewidth',10)
end
if y==1
D1=G.';
end
if y==2
D2=G.';
end
if y==3
D3=G.';
end
if y==4
D4=G.';
end
if y==5
D5=G.';
end
if y==6
D6=G.';
end
if y==7
D7=G.';
end
if y==8
D8=G.';
end
if y==9
D9=G.';
end
if y==10
D10=G.';
end
if y==11
D11=G.';
end
if y==12
D12=G.';
end
if y==13
D13=G.';
end
if y==14
D14=G.';
end
if y==15
D15=G.';
end
if y==16
D16=G.';
end
if y==17
D17=G.';
end
if y==18
D18=G.';
end
if y==19
D19=G.';
end
if y==20
D20=G.';
end
if y==21
D21=G.';
end
if y==22
D22=G.';
end
if y==23
D23=G.';
end
if y==24
D24=G.';
end
if y==25
D25=G.';
end
if y==26
D26=G.';
end
if y==27
D27=G.';
end
if y==28
D28=G.';
end
if y==29
D29=G.';
end
if y==30
D30=G.';
end
if y==31
D31=G.';
end
if y==32
D32=G.';
end
if y==33
D33=G.';
end
if y==34
D34=G.';
end
if y==35
D35=G.';
end
if y==36
D36=G.';
end
end
D=[D1;D2;D3;D4;D5;D6;D7;D8;D9;D10;D11;D12;D13;D14;D15;D16;D17;D18;D19;D20;D21;D22;D23;D24;D25;D26;D27;D28;D29;D30;D31;D32;D33;D34;D35;D36];
end
The script is:
%PLOTTING
close all
clear
load AAI
load AAG
s=1000; %sample size
G=zeros(100000000,1);
I22=zeros(10000000,1);
nr=36;
[I3]=Inhabitants(AAI(1:nr),s);
[D]=AH(nr,AAG,s);
%D=[D1;D2;D3];
figure
HISS=histogram(nonzeros(D));
xlabel('Increase in %')
figure
plot(I3,D,'.')
ylim([0 30])
xlim([0 100])

Accepted Answer

Clayton Gotberg
Clayton Gotberg on 27 Apr 2021
I've reformatted the code in your question to help myself understand it.
function [D]=AH(nr,AAG,s)
for y=1:nr
xx=cell(length(AAG{y}),1);
for o=1:length(AAG{y})
xx{o} =[AAG{y}{o} zeros(1,s-length(AAG{y}{o}))];
end
for k1 = 1:length(AAG{y})
% %Histogram over all values
if k1==1
G(k1:length(xx{k1}))=xx{k1};
else
G((k1-1)*s+1:k1*s)=xx{k1};
% G=[G xx{k1}];
end
%Plotting inhabitants
% scatter(ones(1,numel(xx{k1}))*y, xx{k1},ones(1,numel(xx{k1}))*I{k1},'s','Linewidth',10)
end
if y==1
D1=G.';
end
% and continuing on like that for y==2 to y == 36 and D2 to D36
end
D=[D1;D2;D3;D4;D5;D6...]; % etc
end
This function is much more complicated than it has to be. For example:
function D = AH(nr,AAG,s)
D = [];
for y = 1:nr
data_length = length(AAG{y})
for o = 1:data_length % you can wrap this and the next for loop together
G((o-1)*s+1:o*s) = [AAG{y}{o} zeros(1,s-length(AAG{y}{o}))].';
% I'm not sure why you needed the additional condition here
% since as far as I can tell, G((1-1)*s+1:1*s) evaluates the
% same as G(1:length(xx{k1}))
% Additionally, now G is already flipped.
end
D = [D; G]; % Now, the G from 1:nr is automatically appended to the end of D.
end
end
Finally, the error is because I3 and D end up different sizes, so there might actually be a problem in your "Inhabitants" function or in this one. Without more information (such as the final sizes of D and I3 when the function works and when it does not, and the Inhabitants function) the best advice I can really give is to write out the logic of all your subfunctions to determine exactly what happens to the data (size and transformations) and figure out the difference between what you intend the program to be doing and what you have told the program to do.
I would also recommend you change your variable names, because it's almost impossible to follow what the program is doing. It seems like you're trying to keep the purpose of your program obscured, but it doesn't add any insight to your program if for example:
function dependent_variable = reformat_function(nr,raw_data,expected_length)
dependent_variable = [];
for y = 1:nr
personal_data = [];
data_length = length(raw_data{y})
for o = 1:data_length
personal_data((o-1)*expected_length+1:o*expected_length) = ...
[raw_data{y}{o} zeros(1,s-length(raw_data{y}{o}))].';
end
dependent_variable = [dependent_variable; personal_data];
end
end
Overall, it seems like you're trying to reorder the data from a multilevel cell array; it does feel like there should be a simpler way to do this, though. For example, if AAI{j}{k} corresponds to AAG{j}{k}, you could reformat this for a series of plots simply enough:
figure()
hold on
for ind_1 = 1:nr
plot_x = [];
plot_y = [];
for ind_2 = 1:length(AAI{ind_1})
plot_x = [plot_x AAI{ind_1}{ind_2}];
plot_y = [plot_y AAG{ind_1}{ind_2}];
end
plot(plot_x,plot_y,'.')
end

More Answers (0)

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!