Beginner's roblems with arrays
Mostrar comentarios más antiguos
Hi!
I asked a question here a few days ago and was a bit too hasty in accepting the answer since it only solved a part of my problem.
Basically, I need to combine some values from different arrays:
K = 130;
V = [0.9 0 .5];
THETA = [pi/4 0 3*pi/4];
T = [0 33 57];
So that I have a Vnew and THETAnew that are 1xK and whose values change at T(1:end). So far I've managed to do it crudely like this:
timestep= ones(3,K);
timestep(1,:) = 0:K-1;
timestep(2,1:T(1,2) = V(1,1);
timestep(2,T(1,2)+1:T(1,3) = V(1,2);
timestep(2,T(1,3)+1:end = V(1,3);
timestep(3,1:T(1,2) = THETA(1,1);
timestep(3,T(1,2)+1:T(1,3) = V(1,2);
timestep(3,T(1,3)+1:end = THETA(1,3);
It works, however I'd like to make the code more neat and able to to handle any lengths of V, THETA and T (length(V) = length(THETA) = length(T) will always be true). For this, I've tried the following:
timestep = zeros(3,K);
timestep(1,:) = 0:K-1;
for l = 1:size(T,2)
timestep(2,T(1,l)+1:end) = V(l);
timestep(3,T(1,l)+1:end) = THETA(l);
end
But I cannot get this piece of code to work. MATLAB simply returns an error message when I try to run it.
Any help would be very appreciated.
Respuestas (2)
Sean de Wolski
el 23 de Jun. de 2011
I'm not getting any errors after I add a few parenthesis:
K = 130;
V = [0.9 0 .5];
THETA = [pi/4 0 3*pi/4];
T = [0 33 57];
timestep= ones(3,K);
timestep(1,:) = 0:K-1;
timestep(2,1:T(1,2)) = V(1,1);
timestep(2,T(1,2)+1:T(1,3)) = V(1,2);
timestep(2,T(1,3)+1:end) = V(1,3);
timestep(3,1:T(1,2)) = THETA(1,1);
timestep(3,T(1,2)+1:T(1,3)) = V(1,2);
timestep(3,T(1,3)+1:end) = THETA(1,3);
timestep2 = zeros(3,K);
timestep2(1,:) = 0:K-1;
for l = 1:size(T,2)
timestep2(2,T(1,l)+1:end) = V(l);
timestep2(3,T(1,l)+1:end) = THETA(l);
end
%%Check
isequal(timestep,timestep2)
%ans = 1
7 comentarios
Matt Fig
el 23 de Jun. de 2011
It is only by coincidence that V(2)==THETA(2) that this works.
Morten Jensen
el 23 de Jun. de 2011
Morten Jensen
el 23 de Jun. de 2011
Matt Fig
el 23 de Jun. de 2011
So then the loops won't work. This is because, as I state below, you are assigning:
timestep(3,T(1,2)+1:T(1,3)) = V(1,2);
instead of:
timestep(3,T(1,2)+1:T(1,3)) = THETA(1,2);
If there are more occurrences of this type, looping will be very difficult.
Matt Fig
el 23 de Jun. de 2011
I see you resolved this below...
Morten Jensen
el 23 de Jun. de 2011
Matt Fig
el 23 de Jun. de 2011
See my comments below about the inefficient rewriting going on in this solution.
Matt Fig
el 23 de Jun. de 2011
I cannot see a clear pattern since you are setting
timestep(3,T(1,2)+1:T(1,3)) = V(1,2);
Now if it was THETA(2), that we could work with. Will there always be three rows?
%
%
%
%
EDIT With clarifications in comments...
Here is a way to do it without re-writing parts of the vector over and over. This re-writing could be very inefficient for large arrays. This does assume T(1) is 0.
timestep2 = ones(3,K);
timestep2(1,:) = 0:K-1;
for ii = 1:length(V)-1
timestep2(2,T(ii)+1:T(ii+1)) = V(ii);
timestep2(3,T(ii)+1:T(ii+1)) = THETA(ii);
end
timestep2(2,T(ii+1)+1:K) = V(ii+1);
timestep2(3,T(ii+1)+1:K) = THETA(ii+1);
%
%
%
%
%
EDIT To illustrate the inefficiency discussed above:
K = 130000;
V = rand(1,5000);
THETA = rand(1,5000);
T = [0 sort(round(rand(1,4999)*13000))];
tic
timestep3 = zeros(3,K);
timestep3(1,:) = 0:K-1;
for l = 1:size(T,2)
timestep3(2,T(1,l)+1:end) = V(l);
timestep3(3,T(1,l)+1:end) = THETA(l);
end
toc
tic
timestep2 = ones(3,K);
timestep2(1,:) = 0:K-1;
for ii = 1:length(V)-1
timestep2(2,T(ii)+1:T(ii+1)) = V(ii);
timestep2(3,T(ii)+1:T(ii+1)) = THETA(ii);
end
timestep2(2,T(ii+1)+1:K) = V(ii+1);
timestep2(3,T(ii+1)+1:K) = THETA(ii+1);
toc
isequal(timestep2,timestep3)
%
%
%
I get:
Elapsed time is 4.148093 seconds. % re-writing
Elapsed time is 0.014087 seconds. % not re-writing
2 comentarios
Morten Jensen
el 23 de Jun. de 2011
Matt Fig
el 23 de Jun. de 2011
Ah, I see... Now it makes sense. Please be careful when posting questions in the future. Besides this mistake, you missed a bunch of parenthesis as well...
Categorías
Más información sobre Matrix Indexing en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!