How can I build a Matrix dependencies of a variabel and a matrix? Easy question..

조회 수: 1 (최근 30일)
*Here is an example,
N3 uses N2 and one "k" variable(k is 3 for N3). But I entered k variable by hand. Since I want to find N4 using N3 and "k" (k is 4 for N4), I want "k" goes automaticly...
k starts from 2 and ends whenever I want. I don't want to create another for loop because I have already 2 for loops.And I know there is an easy way.But I couldn't remember it. Something like= N3(:,:,3).*What we_ call *_for these matrices? If you don't know how to this way, can you tell me how to do with for loops?
Thanks and Best Regards..
------------------
N2=zeros(rr-3,rr-2);
for j=1:rr-3
for i=1:rr-2
A1=(X(i+1)-X(i));
A2=(X(i+2)-X(i+1));
if A1==0 && A2==0
N2(j,i)=0;
elseif A1==0 && A2~=0
N2(j,i)=((X(i+2)-t(j))/(A2))*N1(j,i+1);
elseif A2==0 && A1~=0
N2(j,i)=((t(j)-X(i))/(A1))*N1(j,i);
elseif A2~=0 && A1~=0
N2(j,i)=((X(i+2)-t(j))/(A2))*N1(j,i+1)+((t(j)-X(i))/(A1))*N1(j,i);
end
end
------------------------
N3=zeros(rr-3,rr-3);
for j=1:rr-3
for i=1:rr-3
A1=(X(i+2)-X(i));
A2=(X(i+3)-X(i+1));
if A1==0 && A2==0
N3(j,i)=0;
elseif A1==0 && A2~=0
N3(j,i)=((X(i+3)-t(j))/(A2))*N2(j,i+1);
elseif A2==0 && A1~=0
N3(j,i)=((t(j)-X(i))/(A1))*N2(j,i);
elseif A2~=0 && A1~=0
N3(j,i)=((t(j)-X(i))/(A1))*N2(j,i)+((X(i+3)-t(j))/(A2))*N2(j,i+1);
end
end
end
------------------------

채택된 답변

Jan
Jan 2013년 7월 17일
편집: Jan 2013년 7월 17일
If you use N{3} instead of N3 an additional loop would be easy. At least I assume that it would. Unfortunately I cannot understand the first sentence already:
N3 uses N2 and one "k" variable(k is 3 for N3).
What is "one k variable" and where does this appear in the code? What is rr?
You could avoid the inner loop:
N = zeros(rr-3, rr-3);
for j = 1:rr-3
A1 = X(2:rr-1) - X(1:rr-3);
A2 = X(4:rr) - X(2:rr-2);
% A1==0 && A2==0 : N(j,i)=0; Is the default already
index = find(A1==0 & A2~=0);
N(j, index) = ((X(index + 3) - t(j)) ./ A2(index)) * N2(j,index + 1);
index = find(~A1~=0 & A2==0);
N(j, index) = ((t(j) - X(index)) ./ A1(index)) * N2(j,index);
index = find(A1~=0 & ~A2~=0);
N(j, index) = ((t(j) - X(index)) ./ A1(index)) * N2(j,index) + ...
((X(index + 3) - t(j)) / A2(index)) * N2(j, index+1);
end
N{3} = N;
Another approach would be to calculate both matrices and replace NaNs by zeros:
% !For demonstration only! Indices are most likely wrong!
A1 = X(2:rr-1) - X(1:rr-3);
A2 = X(4:rr) - X(2:rr-2);
c1 = bsxfun(@minus, (t ./ A1)' - X ./ A1) .* N2;
c2 = bsxfun(@minus, (X ./ A2)' - t ./ A2) .* N2;
c1(insan(c1)) = 0;
c2(insan(c2)) = 0;
N{3} = c1 + c2;
I did not care about the pile of indices here and I cannot run the versions due to the absence of test data. So treat this as an idea only and implement it by your own.

추가 답변 (1개)

STamer
STamer 2013년 7월 17일
편집: STamer 2013년 7월 17일
Thank you so much sir!
Finally,
N{1} is known, k is integer, rr is integer.However I couldn't apply "find" built-in function instead of elseif statements.Can you help me on it? And what's this bsxfun. Does it do all stuff in one row?
for kk=2:k
N{kk}=zeros(rr-3,rr-kk);
for j=1:rr-3
for i=1:rr-kk
A1=(X(i+kk-1)-X(i));
A2=(X(i+kk)-X(i+1));
if A1==0 && A2==0
N{kk}(j,i)=0;
elseif A1==0 && A2~=0
N{kk}(j,i)=((X(i+kk)-t(j))/(A2))*N{kk-1}(j,i+1);
elseif A2==0 && A1~=0
N{kk}(j,i)=((t(j)-X(i))/(A1))*N{kk-1}(j,i);
elseif A2~=0 && A1~=0
N{kk}(j,i)=((t(j)-X(i))/(A1))*N{kk-1}(j,i)...
+((X(i+kk)-t(j))/(A2))*N{kk-1}(j,i+1);
end
end
end
end
  댓글 수: 7
Jan
Jan 2013년 7월 17일
편집: Jan 2013년 7월 17일
@STamer: I added a personal flag such that I can let Google find it again, when I'm back on my Matlab computer. I see so many messages per day, that I cannot remember where I wanted to add something after a test with Matlab. Sorry for the confusion.
Actually I do not like to sync my bookmarks through the internet. But of course I could sync a list of my visited links by Prism.
Unfortunately I'm too tired now for further experiments. I take a look into the problem tomorrow, if no other contributor has been faster.
STamer
STamer 2013년 7월 18일
Thank you sir.I'm waiting for your response.

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

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by