how to call a specific variable in matrix inside loop process..??
Ältere Kommentare anzeigen
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 Kommentare
Noru
am 1 Mai 2013
It is not actually. What is Q and what does it mean to solve an equation of Q? Just to explain why its is difficult to understand, in your code..
- There is this linedata that is defined nowhere.
- Assuming that it is some array of at least 3 columns, when you write K = linedata(:,3);, K is a column vector with the same number of rows as linedata.
- Same with c1 and c2; these are column vectors with the same number of rows as xdata.
- Your IF statements are not valid a priori, because e.g. c1==bb will generate an array of logicals of the same size as c1, and using this in an IF statement will certainly not produce the effect that you were trying to implement.
- Etc.
Could you write exactly what you are trying to achieve for a few terms without using the loop?
Noru
am 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?
Akzeptierte Antwort
Weitere Antworten (1)
Archit
am 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 Kommentar
Noru
am 1 Mai 2013
Kategorien
Mehr zu Linear Algebra finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!