Logical Arrays and for statements

1 回表示 (過去 30 日間)
Matt
Matt 2012 年 10 月 29 日
My issue is the line commented. To make that line work CNccm needs to be a logical array the size of 2537*357. At the moment i am just getting a 2537*1 array, So over every loop it over writes the value. So basically at the end of the loop i expect to get a 357*1 matrix with values ranging between 0 and 1. Im really stuck if i individually put in values for i when calculating the CNccm value it works fine its just not working with the loop. can someone pleeeease help im getting desperate.
rccm = zeros(357,357);
Crccm = zeros(357,2537);
CNccm = zeros(357,2537);
Nccm = zeros(2537,1);
Prob = zeros(357,1);
for i=1:1:357
for j=1:1:357;
rccm(i,j) = sqrt((0.5*(jmag(i,1)-jmag(j,1)).^2)+((diffmag(i,1)-diffmag(j,1)).^2)+((diffmag2(i,1)-diffmag2(j,1)).^2));
hold on
end
srt = sort(rccm);
r = transpose(srt(21,1:357));
for k=1:1:2537;
Crccm(i,k) = sqrt((0.5*(jmag(i,1)-Cfjmag(k,1)).^2)+((diffmag(i,1)-Cfdiffmag(k,1)).^2)+((diffmag2(i,1)-Cfdiffmag2(k,1)).^2));
end
rcontr = transpose(Crccm);
CNccm = (rcontr(:,i)) <= r(i,1); %Need to make a [2537*357] Logical array
Nccm(i) = sum(CNccm);
Prob(i)= 1-((Nccm(i)/20)*(9/81));
hold on
end

回答 (1 件)

Chris A
Chris A 2012 年 10 月 29 日
See if this works:
CNccm(i,:) = transpose((rcontr(:,i)) <= r(i,1)); % Need to make a
% [2537*357] Logical array
Nccm(i) = sum(CNccm(i,:));
  2 件のコメント
Matt
Matt 2012 年 10 月 30 日
it has given me the answer in type double which is a massive help thanks.. but something is still not going right and i dont understand why.. i should get for Nccm a list of integer numbers ranging in size (Nccm counts up the number if 1's in CNccm) however i get zero for about 337 values then numbers from 338-357 I dont know why its doing this?? if i put a value of 1 in for i and do the calculation manually i get a non zero value.. the error is occuring somewhere in the two above lines..
Matt
Matt 2012 年 10 月 30 日
actually i just realised that i dont want the variable as a double? just a logical array of 2537x357 ??

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by