Cell arrray that stores with iteration a new set of values

  • Thread starter Thread starter marellasunny
  • Start date Start date
  • Tags Tags
    Cell Set
Click For Summary
SUMMARY

The discussion focuses on storing multiple sets of (x, y1) and (x, y2) values in a cell array during iterations in MATLAB. The user is utilizing a predefined function called 'intersections' to find intersections between two functions defined by anonymous functions y1 and y2. The issue arises from overwriting previous values in each iteration, which can be resolved by correctly indexing the cell array. The importance of preallocating arrays with zeros to improve performance is also emphasized.

PREREQUISITES
  • Familiarity with MATLAB programming language
  • Understanding of anonymous functions in MATLAB
  • Knowledge of cell arrays in MATLAB
  • Basic concepts of function intersections
NEXT STEPS
  • Research how to properly index cell arrays in MATLAB
  • Learn about preallocating arrays in MATLAB for performance optimization
  • Explore the use of anonymous functions in MATLAB
  • Study the 'intersections' function and its implementation details
USEFUL FOR

This discussion is beneficial for MATLAB programmers, particularly those working with numerical methods, data storage in iterations, and anyone looking to optimize their code for handling multiple data sets efficiently.

marellasunny
Messages
245
Reaction score
3
Here,I have a pre-defined function called 'intersections(x,...) that finds the intersections between 2 functions of x.Now,for each iteration of the loop,I get a set of (x,y1) and (x,y2) values.
I want to store them in an array without losing the previous iteration's values. So,I figured I'd use a cell array except I can't decipher its working mechanism.Someone,please help me with this.
Have a looksie at the code below.

for i=1:5
for j=1:5
%functions f1 and f2
y1=@(x)C.*(x)./((x.^p)+1);
y2=@(x)(r.*(1-(x./q)));
[a{i,j},b{i,j}] = intersections(x,y1(x),x,y2(x),1);
%a line of plot commands follow[not important]
end
end

I initially used
[xout,yout]= intersections(x,y1(x),x,y2(x),1);
to store the coordinates of the intersections but it kept getting erased out after each iteration.I want the whole data stored coordinates stored in 1 cell array.
 
Physics news on Phys.org
You just need to index the array somehow. For example,

A = zeros(1,5);
for i=1:5
A(i) = i+1;
end
A

This produces a 1x5 array A = [2 3 4 5 6]

If you know the size the array needs to be, it's always better to preallocate with zeros (outside of the loop of course).

Also in MATLAB at least, the index always starts with 1 (never 0)
 

Similar threads

  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
12
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 2 ·
Replies
2
Views
25K