How can I build a Matrix dependencies of a variabel and a matrix? Easy question..
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
*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
------------------------
0 comentarios
Respuesta aceptada
Jan
el 17 de Jul. de 2013
Editada: Jan
el 17 de Jul. de 2013
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.
0 comentarios
Más respuestas (1)
STamer
el 17 de Jul. de 2013
Editada: STamer
el 17 de Jul. de 2013
7 comentarios
Jan
el 17 de Jul. de 2013
Editada: Jan
el 17 de Jul. de 2013
@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.
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!