Effacer les filtres
Effacer les filtres

how to call a specific variable in matrix inside loop process..??

9 vues (au cours des 30 derniers jours)
Noru
Noru le 30 Avr 2013
I have a little problem with my matrix. Perhaps you interested with this..
I have matrix like below :
xdata = [ 1 2 0.0192
1 3 0.0452
2 4 0.0570
3 4 0.0132
2 5 0.0472
2 6 0.0581
4 6 0.0119
5 7 0.0460
6 7 0.0267
6 8 0.0120
6 9 0.0
6 10 0.0
9 11 0.0 ];
I want to call a variable in that matrix one by one following my loop order. For example :
Q = 0;
K = 0;
n = length(xdata);
i = xdata(:,1);
j = xdata(:,2);
for bb = 1:n,
for cc = 1:n,
c1 = xdata(:,1);
c2 = xdata(:,2);
if c1 == bb,
if c2 == cc,
K = linedata(:,3);
end
end
Q = Q + (K*100);
end
end
so when bb = 1 and cc = 2, K will call xdata(:,3) in line 1 = 0.0192
when bb = 1 and cc = 3, K will call xdata(:,3) in line 2 = 0.0452
when bb = 6 and cc = 7, K will call xdata(:,3) in line 9 = 0.0267, and so on...
My code above are wrong and I feel really dizzy when coding it, maybe you can help me with this..
anyone has the solution..?
thanks...
  5 commentaires
Noru
Noru le 1 Mai 2013
ok Cedric, my data above is a linedata from one of a "electrical power bus data system", and now I am studying it with optimization process. To get the best result of the optimization i have to evaluate it. I have a complicated equation and Q are a simple equation that i've been made to represent my evaluation equation.
In my real equation, i have a problem when calling 'xdata(:,3)', because 'xdata(:,3)' have to called when bb = 1:n and cc = 1:n like an example below :
for bb = 1:n,
for cc = 1:n,
Q = Q+sum(K*(A(bb)-B(cc)));
end
end
for an additional data :
A =
0.9148
0.4772
0.9162
0.4969
0.9276
0.7037
0.6946
0.0046
0.5402
0.4130
B =
0.6281
0.5435
0.2160
0.5521
0.8627
0.0049
0.1251
0.7751
0.0204
0.1426
so when bb = 1 and cc = 2, Q = sum(0.0192*(0.9148-0.5435)) and so on...
but when bb = 1 and cc = 1, Q = 0 because there is no K data called in 'xdata'.
is this clear enough..? I am sorry to make you confuse..
Cedric
Cedric le 1 Mai 2013
Modifié(e) : Cedric le 1 Mai 2013
Ok, it is starting to get more clear. In your initial question, the line which defines K defines it as a column vector. In your last comment, it as actually a scalar (the element of xdata(:3) on the row which has its first two elements matching bb and cc). In your last comment, you also write
Q = Q+sum(K*(A(bb)-B(cc)));
in a nested FOR loop. If everything in this equation is scalar, why using SUM? Isn't it instead the following?
Q = Q + K * (A(bb) - B(cc)) ;
with K a number/scalar (0.0192) and A(bb) and B(cc) scalars as well?
Finally, xdata(2,end) is 11, when B has only 10 elements; how should this be managed?

Connectez-vous pour commenter.

Réponse acceptée

Cedric
Cedric le 1 Mai 2013
Modifié(e) : Cedric le 1 Mai 2013
Here is an answer based on what I understood, and a truncated version of xdata in which I removed the last row that contains an invalid index (11) for indexing B.
It seems to me that what you want to do can be reduced to one vector operation as follows:
>> Q = sum( xdata(:,3) .* (A(xdata(:,1)) - B(xdata(:,2))) )
Q =
0.1059
where the SUM operates along dimension 1 (by default). I will discuss it a little more afterwards, but let's check against a corrected version of your double, nested FOR loop:
>> n = size(xdata, 1) ;
>> Q = 0 ;
>> for bb = 1 : n
for cc = 1 : n
id = xdata(:,1)==bb & xdata(:,2)==cc ;
if any(id)
Q = Q + xdata(id,3) * (A(bb) - B(cc)) ;
end
end
end
>> Q
Q =
0.1059
which seems to match to concise version. In these FOR loops, finally only a few iterations lead to a match of indices. If you look at what we are doing in the concise version,
>> A(xdata(:,1))
ans =
0.9148
0.9148
0.4772
0.9162
0.4772
0.4772
0.4969
0.9276
0.7037
0.7037
0.7037
0.7037
is a vector of elements of A that correspond to these matches. Same for B(xdata(:,2)). By subtracting them, we get in one shot all these factors A(bb)-B(cc), that we can multiply element-wise by all the K's ( xdata(:,3) ). Finally, we take the sum of these products.
Test this/these solution(s) little bit by little bit to get full understanding. Let me know if I didn't understand well what you need, or if there is anything that you don't understand.
  1 commentaire
Noru
Noru le 4 Mai 2013
ok thank you Cedric..
your solution really solve my problem..
thanks again..

Connectez-vous pour commenter.

Plus de réponses (1)

Archit
Archit le 1 Mai 2013
a way (maybe a better way exists, but i am not very proficient in matlab yet) to do that is
a=find(xdata(:,1)==2);
% a=[3 5 6];
b=find(xdata(a,2)==4);
% b=[1]
A=xdata(a,1);
B=A(b,3);
% B=0.0570;
basically u filter results by columns till u get ur answer
sorry i cannot think of a more general way to do this at the moment
  1 commentaire
Noru
Noru le 1 Mai 2013
thanks for your solution, but there is an error with your coding
"Attempted to access A(1,3); index out of bounds because size(A)=[3,1]."
and i hope the solution is in 'for loop' operation..
thanks again..

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by