Error using * Inner matrix dimensions must agree.

Hello, im trying to figure out what is the problem (Error in ArduinoComunication v3(i)=v3(1:i)+((v1(1:i)+v1(1:(i-1)))/2)*(x(1:i)-x(1:(i-1))); ), my 2 vectors have the same size. here is part of the code
v1 = zeros(1,tmax*rate);
v2 = zeros(1,tmax*rate);
v3 = zeros(1,tmax*rate);
while t<tmax
t = toc;
% leer del puerto serie
a = fscanf(s,'%f,%f')';
v1(i)=a(1);
v2(i)=a(2);
v3(i)=v3(1:i)+((v1(1:i)+v1(1:(i-1)))/2)*(x(1:i)-x(1:(i-1)));
% dibujar en la figura
x = linspace(0,i/rate,i);
% velocity
set(l1,'YData',v1(1:i),'XData',x);
set(l2,'YData',v2(1:i),'XData',x);
%set(l3,'YData',v3(1:i),'XData',x);
drawnow
% seguir
i = i+1;
end % code
can you guys tell me what im doing wrong thanks

10 个评论

It looks like you mean,
v3(i)=v3(1,i)+((v1(1,i)+v1(1,(i-1)))/2)*(x(1,i)-x(1,(i-1)));
Instead of,
v3(i)=v3(1:i)+((v1(1:i)+v1(1:(i-1)))/2)*(x(1:i)-x(1:(i-1)));
So perhaps you've placed : where you should have placed ,?
Hope this helps!
If that's not the mistake, then there are some problems, as you are trying to multiply a row vector by a row vector, which is not possible (unless you want element-wise, in which case you should use .* rather than *). Additionally, you seem to be subtracting/adding two vectors of different lengths, twice. That is, x(1:i)+x(1:(i-1)) are two vectors of different length that can't be subtracted, you do this similarly with v1.
Good luck!
woow, thank you Now i got this error
Subscript indices must either be real positive integers or logicals.
Error in ArduinoComunication v3(i)=v3(1,i)+((v1(1,i)+v1(1,(i-1)))/2)*(x(1,i)-x(1,(i-1)));
my indices are supposed to be integers
I would guess that you have i = 1 to start off, and when it tries to access v1(1,(i-1)) it's attempting to access v1(1,0) which is not an element. This will also be true for the x(1,(i-1)) line.
You'll need to either skip i=1 or figure out how to handle that case!
well the thing is that im trying to get velocity data from accelerometer (real time), v1(i)=a(1); its the acceleromter data so in 'v3', im trying to do something like this
// velocity
vx = vx+ ((ax + ax0)/2) * (t - t0);
Am i wrong ?
I would start with i=2 and place your initial conditions in v1(1),v2(1),v3(1) - these might be zero if you're starting with no movement.
v1 = zeros(1,tmax*rate);
v2 = zeros(1,tmax*rate);
v3 = zeros(1,tmax*rate);
x= zeros(1,tmax*rate);
i = 2;
t = 0;
% ejecutar bucle cronometrado
tic
while t<tmax
t = toc;
% leer del puerto serie
a = fscanf(s,'%f,%f')';
v1(i)=a(1);
v2(i)=a(2);
v3(i)=v3(1,i)+((v1(1,i)+v1(1,(i-1)))/2)*(x(1,i)-x(1,(i-1)));
% dibujar en la figura
x = linspace(0,i/rate,i);
% velocity
set(l1,'YData',v1(1:i),'XData',x);
set(l2,'YData',v2(1:i),'XData',x);
%set(l3,'YData',v3(1:i),'XData',x);
drawnow
% seguir
i = i+1;
end
still having error
Index exceeds matrix dimensions.
Error in ArduinoComunication (line 47) v3(i)=v3(1,i)+((v1(1,i)+v1(1,(i-1)))/2)*(x(1,i)-x(1,(i-1)));
It looks like your i can grow larger than tmax*rate, which is the length of your v1. Accessing v3(i) when i is larger than tmax*rate can cause this error.
I would try deleting the v3(1,i) at the beginning of that line as it doesn't really contribute anything since v3(1,i) was initialized to 0.
thanks, i did what you said and i changed some parts from my code. this is the new code
v1 = zeros(1,tmax*rate);
v2 = zeros(1,tmax*rate);
v3 = zeros(1,tmax*rate);
i = 2;
t = 0;
% ejecutar bucle cronometrado
tic
while t<tmax
t = toc;
% leer del puerto serie
a = fscanf(s,'%f,%f')';
v1(i)=a(1);
v2(i)=a(2);
% dibujar en la figura
x = linspace(0,i/rate,i);
v3(i)=((v1(1,i)+v1(1,(i-1)))/2)*(x(1,i)-x(1,(i-1)));
% velocity
set(l1,'YData',v1(1:i),'XData',x);
set(l2,'YData',v2(1:i),'XData',x);
%set(l3,'YData',v3(1:i),'XData',x);
drawnow
% seguir
i = i+1;
end % code
I hope it's working and producing good results!
Good luck!

请先登录,再进行评论。

回答(0 个)

类别

帮助中心File Exchange 中查找有关 Startup and Shutdown 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by