Simultaneously Calculating Function from matrix of variables
이전 댓글 표시
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
Fabio Freschi
2019년 10월 14일
any possibility to have fobj? If possible, it could be just a matter of vectorization
osman
2019년 10월 14일
Bob Thompson
2019년 10월 14일
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
2019년 10월 15일
Bob Thompson
2019년 10월 16일
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.
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!