Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

How to refer to elements of a column matrix that is indexed in a loop in matlab?

Status
Not open for further replies.

rameshrai

Member
How to refer to elements of a column matrix that is indexed in a loop in matlab?

Like I want to do operation with the
Code:
for k =1:N;

C(:,k) = [0,1]
...
...
end
how do i refer or extract 0 or 1 of the c(:,k) column matrix in matlab?
how do i refer or extract 0 or 1 of the c:),k) column matrix in matlab?

anybody can help?

thanks
 
It's not clear what you are trying to do, but that code will create a 2xN matrix with all zeros in the first row and all ones in the second row. So, to grab the zeros use C(1,j) and to grab the ones use C(2,j), where j is the column you are interested in.
 
sorry it was not clear,

I have a loop and within the loop I have indexed matrices as follows,
Code:
for k = 1:N
V(:,k) = eig(A(:,:,k));
i need to refer to the eigen values elements of V to calculate

s(k) = v1*log2(v1)+v2*log2(v2)

where v1 and v2 are eigenvalues of V,

how can i compute s(k)?

i tried using following:

s(k) = V(1)*log2(V(1))+V(2)*log2(V(2));

but i am afraid that since there are many s(k) many will refer to same value, in order words i am looking for something like this-

s(k) = V(1,k)*log2(V(1,k))+V(2,k)*log2(V(2,k));

so that we have distinguished eigen values for each iteration k


thanks
 
Yes, the second method matches what I said above. I think that should work.
 
yes, u were right

however, I want to ask how I can change elements of a matrix that is indexed in loop?
eg-
Code:
for k=1:10
V(:,k) = eig(a(:,:,k));
V(:,k)(V(:,k)<0)=2;
end
this didn't work, matab throws error

want to change elements of column vector in each loop k

I doubt that the following way will work during each iteration
Code:
for k=1:10
V(:,k) = eig(a(:,:,k));
V(V<0)=2;
end
V when k=1 and k=2 for example will have the same value, doesn't it?

thanks
 
I'm not sure I understand what you are trying to do. It seems you are trying to check a condition and if that condition is met, then you want to change the value. You also seem to be trying to do this by a vectorized method rather than an if statement. That would actually be a good idea if you put the statement outside the loop, but if you are inside the loop, why not use an if statement? If speed of execution is not critical, I prefer to write loops and if-statements rather than vectorized code.

Still, what you wrote is ambiguous and I cant figure out exactly what you are trying to do.
 
hi, sorry for late reply,

in the above code,

A is a matrix,
eigen value of A is calculated and stored in V
but the eigen values inside V contains some number i want to replace

so the problem is, how to I replace the eigen value?

All this is done within a loop. finding and replacing after the loop statement would do, as the next value would depend on the current one

I hope you can help

thanks
 
If you want to replace an element in an existing matrix, then just access that array element individually. If V has the values you want to replace, then V(2,3) =12; would put 12 in the V array at row 2 and column 3.

Surely you already know this, so I think I'm still not understanding your question.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top