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
Ameerh 2024 年 10 月 4 日
Write a program in MATLAB using for loop read all the elements of the vector and print them.

サインインしてコメントする。

 採用された回答

Star Strider
Star Strider 2017 年 12 月 3 日
Ideally, you would write your function so that it would allow vectorized operations.
If that is not an option try something like this:
Result = zeros(1:length(vector)); % Preallocate
for k1 = 1:length(vector)
Result(k1) = YourFunction(vector(k1));
end
The length call could be replaced by size (with the appropriate dimension argument), or numel. They all have their appropriate uses, depending on your array.

7 件のコメント

Eden Crespo
Eden Crespo 2017 年 12 月 3 日
I get this error "error: A(I) = X: X must have the same size as I"
I have no idea what your function creates.
When in doubt, use a cell array:
Result = cell(size(vector)); % Preallocate
for k1 = 1:length(vector)
Result{k1} = YourFunction(vector(k1));
end
See the documentation on Access Data in Cell Array (link) and related topics to work with the cell array.
Eden Crespo
Eden Crespo 2017 年 12 月 3 日
Thanks for the help! For future reference, why doesn't the arrayfun work on this function
function Name = GetStationName(id) filename = 'divvy-names.csv';
if exist(filename, 'file') ~= 2 %%doesn't exist:
fprintf('**Error in GetStationName: cannot find "divvy-names.csv" file\n');
Name = '?';
return;
end
%%load file of station names, and search for matching id:
%%MATLAB:
%%names = dataset('File', filename, 'Delimiter', ',');
%%LI = (names.ID == id);
%%this works in MATLAB or Octave:
[IDs, Names] = textread('divvy-names.csv', '%d %s', 'delimiter', ',');
LI = (IDs == id);
if sum(LI) == 0 %%not found:
Name = 'Unknown station name';
elseif sum(LI) == 1 %%found match:
I = find(IDs == id); %%what index was match?
Name = Names{I}; %%use { } to extract string @ that index:
else %%found multiple matches, should never happen:
Name = 'Multiple station names found, something is wrong...';
end
end
if true
% code
end
Eden Crespo
Eden Crespo 2017 年 12 月 3 日
nvm I got it to work . Thank you for your help!
Star Strider
Star Strider 2017 年 12 月 3 日
As always, my pleasure!
If my Answer helped you solve your problem, please Accept it!
Ameerh
Ameerh 2024 年 10 月 4 日
Write a program in MATLAB using for loop read all the elements of the vector and print them.
Star Strider
Star Strider 2024 年 10 月 4 日
@Ameerh — Do exactly that. If you have problems, post a new Question with your code and describe the problem you are having with it.
I will not do your homeework for you!

サインインしてコメントする。

その他の回答 (1 件)

Stephen23
Stephen23 2017 年 12 月 3 日
編集済み: Stephen23 2017 年 12 月 3 日
Either use a for loop or arrayfun:
vec = [234,456,687,643,23,45];
arrayfun(@fun,vec)
You might also like to read this:

3 件のコメント

Eden Crespo
Eden Crespo 2017 年 12 月 3 日
I get this error when trying the arrayfun "error: arrayfun: all values must be scalars when UniformOutput = true" any Idea what the problem is?
Stephen23
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
Eden Crespo 2017 年 12 月 3 日
Sorry I am very new to this. Ill try It thank you!

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeFunctions についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by