Pulling specific data from a matrix, and starting a new matrix
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hello,
I am currently using MATLAB 2010 and am having difficulty figuring out how to accomplish a certain task.
This is a for loop with if statement that I have in a matlab .m file I am currently working on.
for i=1:1:(t_eff((AR_stop+1)-AR_start)) if T_eff(i) > (T_avg+273.15);
T(i) = T_eff(i);
end end
In this loop, i=261 steps. One strange issue I am having is that I can't get T(i) to equal a matrix with 261 items; it only goes up to 260. That is a separate issue, however.
After I have formed matrix T, I want to run it through another loop or something so that I can pull specific data out of T.
The loop above returns a matrix T that is a matrix elements that have a value of 0 or a specific integer when T_eff(i) > (T_avg+273.15).
What I need to do is pull the data when the value in the matrix is not 0 so that I can determine how many elements that are not zero there are. The number of elements corresponds with the number of seconds the experiment I did took place over, so I need to be able to go into that matrix to find it (I will probably use the length function).
My question is how do I do this? How do I pull specific elements from one matrix and start another with it? How do I tell MATLAB that I want to take any value >0 from matrix T and put it into a separate matrix (named T_AR, for example)?
My end goal is to end up with a matrix that only includes values when T_eff(i) > (T_avg+273.15) from the original loop.
Thanks!
0 comentarios
Respuestas (2)
Matt Fig
el 8 de Mzo. de 2011
Use the NNZ function to count the number of nonzeros. Use logical indexing to get the nonzeros.
nzA = A(A~=0)
0 comentarios
Walter Roberson
el 8 de Mzo. de 2011
For the first issue: pre-allocate your T. Not only will it be more efficient, but you will eliminate the difficulty you are having where some number of T_eff at the end do not happen to match the condition and thus do not extend T to the needed length.
To determine the number of nonzero values in the matrix, use nnz()
To extract the non-zero values from T into T_AR, use
T_AR = T(logical(T))
Or equivalently but marginally slower,
T_AR = T(T ~= 0);
Your goal could be met in one step by using
T_AR = T_eff(T_eff(1:t_eff((AR_stop+1)-AR_start) > (T_avg+273.15))
though I would probably use a temporary variable to hold the upper bound for clarity.
Question: should the upper bound of your loop be based on t_eff() or on T_eff() ?? It is confusing if you have two variables that differ only in the upper/lower case of the first letter.
3 comentarios
Walter Roberson
el 8 de Mzo. de 2011
T = zeros(1, t_eff((AR_stop+1)-AR_start) );
That is, before you start the loop, initialize T to be the maximum size it could end up. This keeps Matlab from continually having to make the matrix bigger as you write new elements in to it, and it deals with the case like you had where the last T_eff does not satisfy the condition, leaving you not writing in to the final T and thus ending up with a T that was too short.
Ver también
Categorías
Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!