Simultaneously Calculating Function from matrix of variables
Mostra commenti meno recenti
Hello there! I have variables in matrix D, there are 5 columns in each row and the column has values for 5 variables. I am using the following codes for calculation.
for jj=1:1:size(D(:,1))
mete(jj)=fobj(D(jj,:));
end
In this code, fobj is running a function file by using the lines of matrix D. But it takes longer time to calculate than I want when I increased the size of the matrix. I would like to use these variables not line by line but I need to use them as matrix. how could I calculate the function values for all the lines of variables in a very short time as a matrix instead of using "for loop"?
5 Commenti
Fabio Freschi
il 14 Ott 2019
any possibility to have fobj? If possible, it could be just a matter of vectorization
osman
il 14 Ott 2019
Bob Thompson
il 14 Ott 2019
Wouldn't that function work just as well for 2D as it does for a 1D vector? If all you're doing is squaring the individual elements, it shouldn't matter if you feed it one element, one line, one sheet, or any size matrix. It should be able to perform the function no matter how much you pass into it.
osman
il 15 Ott 2019
Bob Thompson
il 16 Ott 2019
Being able to vectorize an operation (which allows you to evaluate all at once instead of in a for loop) is dependant on the operation you're trying to perform. Your example showed that you were doing a squaring operation, which is already set up to be vectorized, so you don't need to have your for loop at all for that example.
If the example is not your actual operation, then we will need you to post it to determine if it can be vectorized or not.
Risposte (0)
Categorie
Scopri di più su Matrices and Arrays in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!