Logical Arrays and for statements

3 次查看(过去 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 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