I have a function which relies on a few inputs, all of these inputs will be the same for each iteration apart from one which will be changing. I want to output each iteration of the loop to a different array.
I've tried doing it using a cell array as below.
Here's an example of the code.
for b = [0:0.1:5]
cell_array{b} = function(a,b,c)
end
With this I get an error saying: 'Cell contents indices must be greater than 0' which I assume is because of the '0' in vector b. If I alter it to be:
for b = [0:0.1:5]
cell_array{1:51} = function(a,b,c)
end
I get the error saying: 'Expected one output from a curly brace or dot indexing expression, but there were 5 results.'
Is there a better way of getting each iteration to output separately?

 채택된 답변

Star Strider
Star Strider 2018년 1월 1일
Try this:
fun = @(a,b,c) a + b - c; % Create Function
a = 3; % Create Data
c = 11; % Create Data
for b = 1:6
cell_array{b} = fun(a, b-1, c);
end

댓글 수: 4

I should've said before in my actual code the values I want to change are not integers so doing what you proposed gives me a subscript error.
MATLAB subscripts are integers greater than zero.
It would help tremendously if I did not have to guess what you are thinking.
Try this:
fun = @(a,b,c) a + b - c; % Create Function
a = 3; % Create Data
b = rand(1,6); % Create Data
c = 11; % Create Data
for k = 1:6
cell_array{k} = fun(a, b(k), c);
end
Apologies for that, it slipped my mind.
It did work though so thanks for that. Nice solution aswell.
As always, my pleasure.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

2018년 1월 1일

댓글:

2018년 1월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by