Error while using a function instead of a .csv file

n = 5:40; %these are the limits of the function
c=2*n; %the function
a = (1-0.7)/(1-(0.7)^5);
w=a*(0.7).^[0:4];
for k = n
avg(k) = sum(fliplr(w)'.*c(k-4:k)); %this is the part where the error occurs
end
plot(n,c(5:40),':o');
hold on
plot(n,avg(5:40),'-x');
I get an error saying "Unable to perform assignment because the left and right sides have a different number of elements." when I run the code using the function (c=2*n) , but if I use a .csv file with the exact same values, instead of the function, the code works perfectly.

2 comentarios

I fixed some bugs in your code. What do you mean by "if I use a .cvs file"? Functions and .cvs files cannot be used in the same way. Perhaps you mean "when I import (which variable?) from a .cvs file"?
Basically what I did was I used the data from the equation to produce a sheet in excel of the values ranging from the values of n=5 upto n=40, then imported this into matlab and used it to create the graph

Iniciar sesión para comentar.

Respuestas (1)

Riccardo Scorretti
Riccardo Scorretti el 2 de Mayo de 2022
Editada: Riccardo Scorretti el 2 de Mayo de 2022
n = 5:40; % these are the limits of the function
n runs from 5 to 40: you are bound to have problems when trying to evaluate c(n-4:n) because c is a vector of size 36. I guess the solution is to 0-pad the vector c:
c=[0 0 0 0 2*n];
% c=2*n; % the function
a = (1-0.7)/(1-(0.7)^5);
w=a*(0.7).^[0:4];
for k = n
w' is a column vector (because w is a row vector) and c is a row vector, hence the product fliplr(w)'.*c(k-4:k) is a matrix, and the expression you want to evaluate is a row vector which contains the sum of columns of that matrix. Remove the trasposition:
% avg(k) = sum(fliplr(w)'.*c(k-4:k)); % this is the part where the error occurs
avg(n) = sum(fliplr(w) .*c(n-4:n)); % this is the part where the error occurs
end
plot(n,c(5:40),':o');
hold on
plot(n,avg(5:40),'-x');

3 comentarios

Unfortuantely the graph I am looking for would look something like this:
I did this using the .csv file method I mentioned earlier
n = 5:40;
c=csvread('QQQdata.csv') %the part where I use a csv file
a = (1-0.7)/(1-(0.7)^5);
w=a*(0.7).^[0:4];
for k = n
avg(k) = sum(fliplr(w)'.*c(k-4:k));
end
plot(n,c(5:40),':o');
hold on
plot(n,avg(5:40),'-x');
This is what the .csv file looked like
Thanks for the help though.
Riccardo Scorretti
Riccardo Scorretti el 2 de Mayo de 2022
Editada: Riccardo Scorretti el 2 de Mayo de 2022
Can you share the file .csv?
Its alright, I got the answer to the question (c in this case was a column vector and not a row one like the .csv file), Thanks for the help though.

Iniciar sesión para comentar.

Categorías

Más información sobre Environment and Settings en Centro de ayuda y File Exchange.

Productos

Versión

R2021b

Etiquetas

Preguntada:

el 2 de Mayo de 2022

Comentada:

el 2 de Mayo de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by