Beginner's roblems with arrays

조회 수: 7 (최근 30일)
Morten Jensen
Morten Jensen 2011년 6월 23일
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.

답변 (2개)

Sean de Wolski
Sean de Wolski 2011년 6월 23일
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
Morten Jensen
Morten Jensen 2011년 6월 23일
It was my mistake..
It is supposed to be like this:
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)) = THETA(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);
Matt Fig
Matt Fig 2011년 6월 23일
See my comments below about the inefficient rewriting going on in this solution.

댓글을 달려면 로그인하십시오.


Matt Fig
Matt Fig 2011년 6월 23일
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
Morten Jensen
Morten Jensen 2011년 6월 23일
Yes, a mistake from my side. and yes, there will always be 3 rows. only length can change.
Matt Fig
Matt Fig 2011년 6월 23일
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...

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by