I make mistakes while creating a matrix
1 view (last 30 days)
Show older comments
DNdog =[2;2;2;3;3;3;5;5]
BNdog =[3;5;6;2;5;6;2;3]
blnmynokta =[2;5;6]
aik0 =[0.289528073170415
0.330368386539138
0.339433756993357
0.289527607389207
0.407152568064959
0.424260572052654
0.330366215572373
0.407154361068522]
bik0 =[-0.289528073170415
-0.330368386539138
-0.339433756993357
-0.289527607389207
-0.407152568064959
-0.424260572052654
-0.330366215572373
-0.407154361068522]
for i = 1:length(DNdog)
for j = 1:(length(blnmynokta))
if DNdog(i) == blnmynokta(j)
Adog1(i,2*j-1:2*j) = [aik0_dog(i,1) bik0_dog(i,1)]
elseif BNdog(i) == blnmynokta(j)
Adog1(i,2*j-1:2*j) = [-aik0_dog(i,1) -bik0_dog(i,1)]
else
Adog1(i,2*j-1:2*j) = [0 0]
end
end
end
% This is the resulting matrix
Adog1=[0.289528073170415 -0.289528073170415 0 0 0 0
0.330368386539138 -0.330368386539138 -0.330368386539138 0.330368386539138 0 0
0.339433756993358 -0.339433756993358 0 0 -0.339433756993358 0.339433756993358
-0.289527607389207 0.289527607389207 0 0 0 0
0 0 -0.407152568064959 0.407152568064959 0 0
0 0 0 0 -0.424260572052654 0.424260572052654
-0.330366215572373 0.330366215572373 0.330366215572373 -0.330366215572373 0 0
0 0 0.407154361068522 -0.407154361068522 0 0]
% This is the matrix I have to find. But I could not.
ADOG=[0.289528073170415 0 0
0.330368386539138 -0.330368386539138 0
0.339433756993358 0 -0.339433756993358
-0.289527607389207 0 0
0 -0.407152568064959 0
0 0 -0.424260572052654
-0.330366215572373 0.330366215572373 0
0 0.407154361068522 0]
9 Comments
Accepted Answer
Geoff Hayes
on 8 Apr 2017
Muhendisleksi - I think that you can simplify your code to populate Adog1 to be the following
Adog1 = zeros(length(DNdog),length(blnmynokta)); % pre-size Adog1
for i = 1:length(DNdog)
for j = 1:(length(blnmynokta))
if DNdog(i) == blnmynokta(j) % if the coefficient matches then put in Adog1
Adog1(i,j) = aik0(i);
end
if BNdog(i) == blnmynokta(j) % do the same comparison for BNdog
Adog1(i,j) = bik0(i);
end
end
end
Try the above and see what happens!
More Answers (1)
Andrei Bobrov
on 8 Apr 2017
Adog1 = sum((reshape([DNdog,BNdog],[],1,2) == blnmynokta(:)').*cat(3,1,-1).*aik0,3);
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!