How can I write a loop that performs a function to each element in a vector?
이전 댓글 표시
say I have a vector with numbers 234 456 687 643 23 45 and have a function which can only take one value at a time. How would I loop to call the function to each one of the values individually?
댓글 수: 1
Ameerh
2024년 10월 4일
Write a program in MATLAB using for loop read all the elements of the vector and print them.
채택된 답변
추가 답변 (1개)
vec = [234,456,687,643,23,45];
arrayfun(@fun,vec)
You might also like to read this:
댓글 수: 3
Eden Crespo
2017년 12월 3일
Stephen23
2017년 12월 3일
@Eden Crespo: your function returns non-scalar output arguments. Simply read the error message and do exactly what it states, that will solve the problem:
arrayfun(@fun,vec,'UniformOutput',false)
Eden Crespo
2017년 12월 3일
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!