How to overwrite part of a vector? - matlab

I am trying to rewrite part of a vector given that:
t = -10:.1:10
x = exp((-3.*t);
The length of x will be 201, and I want to rewrite the first 100 values.
The only way I've gotten to work is by doing this:
EDIT Fixed typo.
t = 0:.1:10;
x = exp((-3.*t); % EDIT: THERE WAS A TYPO HERE
z = zeros(1,100);
for k = 1 : 100
x(k) = z(k);
end
There are three questions. First: What is a faster and more efficient way of doing this? Second: What do I do if I don't want to overwrite the first part of the code but rather the middle or the second part? Third: Is there a way of utilizing the full range of t where t = -10:.1:10 and just ignoring the first half instead of writing a whole new variable for it?
First: Nothing else I've tried has been successful.
Second: The only way I can think to do that is to append the two vectors together, but then it doesn't overwrite the data, so that is a no go.
Third:I have tried an if statement and that didn't work at all.

Your code appears to assign something to y, then changes the value of x. I assume that is a typo - and not the problem you actually want to fix.
In general, if you have
t = -10:0.1:10; % my preference: t = linspace(-10,10,201);
and
y = exp(-3 * t );
but you want to set the first 100 elements of y to zero, you can then do
y(1:100) = 0;
If you wanted never to compute y(1:100) in the first place you might do
y = zeros(size(t));
y(101:end) = exp(-3 * t(101:end));
There are many variations on this. I think the above code samples address all three of your questions.

change your
for k = 1 : 100
x(k) = z(k); % i think it should be y(k) though
end
to
x(1:100) = 0;

You could use logical indexing; that is, you can use a logical statement to select elements of a vector/matrix:
t = -10:0.1:10;
x = exp((-3.*t);
x(t < 0) = 0;
This works for the middle of a matrix too:
x(t > -5 & t < 5) = whatever;

Related

Why is this for loop giving me an error?

So I am trying to go through a for loop that will increment .1 every time and will do this until the another variable h is less than or equal to zero. Then I am suppose to graph this h variable along another variable x. The code that I wrote looks like this:
O = 20;
v = 200;
g = 32.2;
for t = 0:.1:12
% Calculate the height
h(t) = (v)*(t)*(sin(O))-(1/2)*(g)*(t^2);
% Calculate the horizontal location
x(t) = (v)*(t)*cos(O);
if t > 0 && h <= 0
break
end
end
The Error that I keep getting when running this code says "Attempted to access h(0); index must be a positive integer or logical." I don't understand what exactly is going on in order for this to happen. So my question is why is this happening and is there a way I can solve it, Thank you in advance.
You're using t as your loop variable as well as your indexing variable. This doesn't work, because you'll try to access h(0), h(0.1), h(0.2), etc, which doesn't make sense. As the error says, you can only access variables using integers. You could replace your code with the following:
t = 0:0.1:12;
for i = 1:length(t)
% use t(i) instead of t now
end
I will also point out that you don't need to use a for loop to do this. MATLAB is optimised for acting on matrices (and vectors), and will in general run faster on vectorised functions rather than for loops. For instance, your equation for h could be replaced with the following:
O = 20;
v = 200;
g = 32.2;
t = 0:0.1:12;
h = v * t * sin(O) - 0.5 * g * t.^2;
The only difference is that you have to use the element-wise square (.^2) rather than the normal square (^2). This means that MATLAB will square each element of the vector t, rather than multiplying the vector t by itself.
In short:
As the error says, t needs to be an integer or logical.
But your t is t=0:0.1:12, therefore a decimal value.
O = 20;
v = 200;
g = 32.2;
for t = 0:.1:12
% Calculate the height
idx_t = 1:numel(t);
h(idx_t) = (v)*(t)*(sin(O))-(1/2)*(g)*(t^2);
% Calculate the horizontal location
x(idx_t) = (v)*(t)*cos(O);
if t > 0 && h <= 0
break
end
end
Look this question's answer for more options: Subscript indices must either be real positive integers or logical error

What is wrong with my Simpson algorithm?

I was trying to write an algorithm to approximate integrals with Simpson's method. When I try to plot it in a loglog plot, however, I don't get the correct order of accuracy which is O(h^4) (I get O(n)). I can't find any errors though. This is my code:
%Reference solution with Simpson's method (the reference solution works well)
yk = 0;
yj = 0;
href = 0.0001;
mref = (b-a)/href;
for k=2:2:mref-1
yk=yk+y(k*href+a);
end
for j=1:2:mref
yj=yj+y(href*j+a);
end
Iref=(href/3)*(y(a)+y(b)+2*yk+4*yj);
%Simpson's method
iter = 1;
Ehmatrix = 0;
for n = 0:nmax
h = b/(2^n+1);
x = a:h:b;
xodd = x(2:2:end-1);
xeven = x(3:2:end);
yodd = y(xodd);
yeven = y(xeven);
Isimp = (h/3)*(y(x(1))+4*sum(yodd)+2*sum(yeven)+y(b));
E = abs(Isimp-Iref);
Ehmatrix([iter],1) = [E];
Ehmatrix([iter],2) = [h];
iter = iter + 1;
end
figure
loglog(Ehmatrix(:,2),Ehmatrix(:,1))
a and b are the integration limits and y is the integrand that we want to approximate.
Djamillah - your code looks fine though the initialization of h is probably valid only for the case where a==0, so you may want to change this line of code to
h = (b-a)/(2^n+1);
I wonder if x = a:h:b; will always be valid - sometimes b may be included in the list, and sometimes it might not be, depending upon h. You may want to reconsider and use linspace instead
x = linspace(a,b,2^n+1);
which will guarantee that x has 2^n+1 points distributed evenly in the interval [a,b]. h could then be initialized as
h = x(2)-x(1);
Also, when determining the even and odd indices, we need to ignore the last element of x for both even and odd. So instead of
xodd = x(2:2:end-1);
xeven = x(3:2:end);
do
xodd = x(2:2:end-1);
xeven = x(3:2:end-1);
Finally, rather than using a vector y (how is this set?) I might just use the function handle to the function that I'm integrating instead and replace the calculation above as
Isimp = delta/3*(func(x(1)) + 4*sum(func(xodd)) + 2*sum(func(xeven)) + ...
func(x(end)));
Other than these tiny things (which are probably negligible), there is nothing in your algorithm to indicate a problem. It produced similar results to a version that I have.
As for the order of convergence, should it be O(n^4) or O(h^4)?
Taking into account Geoff's suggestions, and making a few other changes, it all works as expected.
%Reference solution with Simpson's method (the reference solution works well)
a=0;
b=1;
y=#(x) cos(x);
nmax=10;
%Simpson's method
Ehmatrix = [];
for n = 0:nmax
x = linspace(a,b,2^n+1);
h = x(2)-x(1);
xodd = x(2:2:end-1);
xeven = x(3:2:end-1);
yodd = y(xodd);
yeven = y(xeven);
Isimp = (h/3)*(y(x(1))+4*sum(yodd)+2*sum(yeven)+y(b));
E = abs(Isimp-integral(y,0,1));
Ehmatrix(n+1,:) = [E h];
end
loglog(Ehmatrix(:,2),Ehmatrix(:,1))
P=polyfit(log(Ehmatrix(:,2)),log(Ehmatrix(:,1)),1);
OrderofAccuracy=P(1)
You were getting O(h) accuracy because xeven=x(3:2:end) was wrong. Replacing it by xeven=x(3:e:end-1) fixes the code, and thus the accuracy.

Matlab: How to re-order (re-organize) a matrix

I have a random column matrix:
r = rand(1,300)';
I want to re-order it so that instead of having elements in the order of 1,2,3,...,300
I will have elements 1,11,21,31,...,291,2,12,22,32,...,292,3,13,33,...293,...,300.
In other words, I want to take every 10th value, beginning with 1 and put them in that order, then do the same for 2 with every 10th value. I know one way to do this is:
n = 10;
r = [r(1:n:numel(r)); r(2:n:numel(r)); r(3:n:numel(r));...;r(10:n:numel(r))]; % Skipped 4-9 in this example
But obviously, this is very cumbersome to do more than a couple of times. Is there something more efficient?
A loop should be easy, but I am not doing it correctly, it seems (I can see why this might not work, but I can't correct it).
(Here is what I tried:)
n = 10;
for i = 1:10
a = [r(i:n:numel(r))];
end
Any suggestions or help is greatly appreciated.
You can do it like this:
r = reshape(reshape(r, 10, 30)', 300, 1)
EDIT:
As pointed out by #LuisMendo on the comments, it's safer to use .' than ' to transpose the matrix, because if the matrix is complex, that could introduce a complex conjugation. Then, it would be safer to do it like this:
r = reshape(reshape(r, 10, 30).', 300, 1)
You could reshape it into 30x10 matrix, transpose, and take the flat index:
A = 1:300;
A = reshape(A,30,10);
A = A';
A = A(:);
Try this -
intv = 10; %%// Interval after which you intend to get the values consecutively
out = r(reshape(reshape(1:numel(r),intv,[])',1,[]))
Some of the other solutions posted are more efficient, but your idea was a good one. It requires a simple fix to work:
N = numel(r);
M = N/10;
a=[];
for ii = 1:M
a= [a r(ii:10:N)];
end
Hope this helps

Compute the convolution of two arrays in MATLAB

I am trying to generate an array from some starting values using this formula in MATLAB:
yt = a0 + ∑i=1p (ai ⋅ yt-i), t ≥ p
p is some small number compared to T (max t). I have been able to make this using two for cycles but it is really slow. Is there some easy way to do it?
First p values of y are provided and vector a (its length is p+1) is provided too...
This is what I have so far, but now when I tried it, it doesn't work 100% (I think it's because of indexing from 1 in MATLAB):
y1 = zeros(T+1, 1);
y1(1:p) = y(1:p);
for t = p+1:T+1
value = a1(1);
for j = 2:p+1
value = value + a1(j)*y1(t-j+1);
end
y1(t) = value;
end
EDIT: I solved it, I am just not used to Matlab indexing from 1...
This statement
if(p>=t)
looks odd inside a loop whose index expression is
for t = p+1:T+1
which seems to guarantee that t>p for the entire duration of the loop. Is that what you meant to write ?
EDIT in response to comment
Inside a loop indexed with this statement
for j = 2:p
how does the reference you make to a(j) ever call for a(0) ?
y1 = zeros(T+1, 1);
y1(1:p) = y(1:p);
for t = p+1:T+1
value = a1(1);
for j = 2:p+1
value = value + a1(j)*y1(t-j+1);
end
y1(t) = value;
end

MATLAB and function

I want to calculate the function y(t) from the equation:
y(t) = -3t^2+5, t>=0
y(t) = 3t^2+5, t<0
for -9 <= t <= with the step-size 0.5
And I want to plot it by using MATLAB. I approach this question in two ways:
First
t=0:0.5:9
y=-3*t^2+5
t1=-0.00000000001:0.5:-9
y1=3*t^2+5
plot(t,y,t1,y1)
Second by using loop
t=-9:0.5:9
if(t>=0)
y=-3*(t.^2)+5
else
y=3.*(t.^2)+5
end
plot(t,y)
My problem is the two ways above seem not to give the same answer... Which one is the correct answer?
You can use the sign function to do this particular example a little easier:
t = -9:0.5:9;
y = -sign(t)*3.*t.^2 + 5;
plot(t,y);
In your first attempt, your t1 definition should be:
t1 = 0:-0.5:-9;
Note the minus sign on the increment.
Using a "loop" you seem to have left out the actual loop part. Try something like
t = -9:0.5:9;
for idx = 1:length(t)
if t(idx) <= 0
y(idx) = -3*(t(idx).^2)+5
etc.
Here's a more idiomatic solution that avoids SIGN for cases where that's not the only difference.
t = -9:0.5:9
y = -3*t.^2+5
y(t<0) = 3*t(t<0).^2+5
plot(t, y)