why the real matrix becomes complex number when transfered to cell

1 vue (au cours des 30 derniers jours)
妮芸
妮芸 le 29 Nov 2023
Modifié(e) : 妮芸 le 29 Nov 2023
why the real matrix becomes complex number when transfered to cell?
many thanks to all !
This is all of my code operations. FD is a matrix with all real number.
I actually want to get the result of fincost, but I found that the final output (fin_cost andTermfin_cost)contained complex numbers.
I use the code “isreal” to distinguish and finally find that FY contains complex numbers.
please help me see what the problem is.
Thanks to all !!
S=67
N=45% I'm sorry for a mistake that S and N are equal to 67 and 45 repectively.
FY=cell(S,S)
for i=1:1:S
for j=1:1:S
FY{i,j}=FD((i-1)*N+1:i*N,j)
end
end
containsComplex=~isreal(FY);
if containsComplex
disp('complex。');
else
disp('real。');
end
FY_shape=cell(S,S)
for i=1:1:S
for j=1:1:S
FY_shape{i,j}=FY{i,j}(1:44)
end
end
fin_cost=cell(S,1)
for i=1:1:S
fin_cost{i,1}=zeros(44,1)
for j=1:1:S
term3=FY_shape{i,i}.*FY_shape{j,j}
term4=FY_shape{i,j}.^2
term4(term4 == 0)=epsilon;
fin_cost{i,1}=fin_cost{i,1}+(term3./term4).^(1/14)
end
end
Termfin_cost= cell2mat(fin_cost)
  3 commentaires
Walter Roberson
Walter Roberson le 29 Nov 2023
We recommend that you do not use i or j for loop control variables, as those are both also sqrt(-1)
Dyuman Joshi
Dyuman Joshi le 29 Nov 2023
There does not seem to be any complex number in the final output.
All values stored in the cell array are real, see below -
load('FD.mat');
%Check for values in FD
all(isreal(FD), 'all')
ans = logical
1
S=45;
N=6;
FY=cell(S,S);
for i=1:1:S
for j=1:1:S
FY{i,j}=FD((i-1)*N+1:i*N,j);
end
end
FY
FY = 45×45 cell array
Columns 1 through 12 {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} Columns 13 through 24 {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} Columns 25 through 36 {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} Columns 37 through 45 {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double}
%Check for values in FY
all(cellfun(@(x) all(isreal(x)), FY), 'all')
ans = logical
1

Connectez-vous pour commenter.

Réponses (1)

Chunru
Chunru le 29 Nov 2023
You won't get complex output if FD is real. Check type of FD.
S=15; %smaller
N=6;
FD = rand(N*S, S);
FY=cell(S,S)
FY = 15×15 cell array
Columns 1 through 12 {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} Columns 13 through 15 {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double}
for i=1:1:S
for j=1:1:S
FY{i,j}=FD((i-1)*N+1:i*N,j);
end
end
FY
FY = 15×15 cell array
Columns 1 through 12 {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} Columns 13 through 15 {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double} {6×1 double}
whos
Name Size Bytes Class Attributes FD 90x15 10800 double FY 15x15 34200 cell N 1x1 8 double S 1x1 8 double cmdout 1x33 66 char i 1x1 8 double j 1x1 8 double

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by