for i=1:n-2
for j=i+2:n
k_arr=i+1:j-1
for counter = 1:length(k_arr)
k = k_arr(counter);
if all(w(k)<(w(i)+((w(j)-w(i))*(t(k)-t(i))/(t(j)-t(i)))))
A(i,j)=1;
A(j,i)=1;
else A(i,j)=0;
A(j,i)=0;
end
disp('cycle completed');
end
end
In output I am expecting 1 only when if condition is true for all elements from k_arr(array) but it is giving output 1 even single element of array satisfy the condition. Please help me

 채택된 답변

Walter Roberson
Walter Roberson 2021년 10월 31일

0 개 추천

if all(w(k)<(w(i)+((w(j)-w(i))*(t(k)-t(i))/(t(j)-t(i)))))
You are indexing all variables there, and each time you are indexing with a scalar. Because of this, the part inside the all() is always a scalar result, so the all() is not doing anything useful for you there.
Now, if you were to do
for i=1:n-2
for j=i+2:n
k_arr=i+1:j-1
k = k_arr;
if all(w(k) < (w(i)+((w(j)-w(i))*(t(k)-t(i))/(t(j)-t(i)))))
A(i,j)=1;
A(j,i)=1;
else
A(i,j)=0;
A(j,i)=0;
end
end
end

댓글 수: 3

Beeraiah Thonti
Beeraiah Thonti 2021년 10월 31일
Thank you I tried this and this working for array(test case I tried) but when I applied this for a data file(txt file contains column matrix) it is giving error al below
Error using <
Matrix dimensions must agree.
Error in visalgo (line 26)
if all(w(k)<(w(i)+((w(j)-w(i))*(t(k)-t(i))/(t(j)-t(i)))))
Could you please solve this
I predict that your w and your t do not have the same orientation.
for i=1:n-2
for j=i+2:n
k_arr=i+1:j-1
k = k_arr;
if all(reshape(w(k),[],1) < (w(i)+((w(j)-w(i))*(reshape(t(k),[],1)-t(i))/(t(j)-t(i)))))
A(i,j)=1;
A(j,i)=1;
else
A(i,j)=0;
A(j,i)=0;
end
end
end
Beeraiah Thonti
Beeraiah Thonti 2021년 10월 31일
Brilliant how couldd I miss a basic thing Thank you

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

추가 답변 (0개)

카테고리

제품

릴리스

R2015a

질문:

2021년 10월 31일

댓글:

2021년 10월 31일

Community Treasure Hunt

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

Start Hunting!

Translated by