How to solve equations with data stored in a cell arrays

Hi I am trying to solve the following equation: Eqn = (a/2)*log((Rs+a)/(Rs-a))== Ind; I can solve it for individual values of Ind using the functions solve as follows:
Ind = 13;
Rs = 5;
syms a
Eqn = (a/2)*log((Rs+a)/(Rs-a))== Ind;
Sol = solve(Eqn,a);
The problem comes when Ind data are stored in a cell array cell(1,n) and each cell contains (m x n) arrays. I would like to solve the equation for the first column of the array contained at each cell and organize the solution also in a cell array where sol{i}(1:end) comes from Ind{i}(1:end,1) I have tried:
Ind = cell(1,10);
Sol = cell(1,10);
Eqn = cell(1,10);
for i = 1:10
Ind{i} = (0:3:15)';
end
Rs = 5;
for i = 1:10
syms a
Eqn{i} = (a/2)*log((Rs+a)/(Rs-a))== Ind{i}
Sol{i} = solve(Eqn{i},a);
end
But of course it does not work. I have also tried other combination and even indexing the data with two for loops taking i and j for numbering cell number and row but I just have no idea how to treat these kind of data. Thanks a lot in advance for any advice!!!

 Accepted Answer

I would simply do this:
syms a
Ind = 13;
Rs = 5;
Ind = 0:3:15;
for i = 1:numel(Ind)
Sol{i} = solve((a/2)*log((Rs+a)/(Rs-a))== Ind(i),a);
end

4 Comments

MaAdVi's "Answer" moved here:
Hi! thanks for answering so quickly. The code works for Ind being a matrix but for me Ind is a cell array with 10 different columns where each column contains a (nx1) array, but they are not the same one, sorry if I have mislead you with my example before. Let's take Ind as
Ind=cell(1,10);
for i=1:10
Ind{i} = (1:i:20)';
end
Do I need to set an extra loop to access every column of the cell array? (I have tried but it does not work. or do I necessarily have to solve it separately? THANKS!
My pleasure.
See the documentation on: Access Data in Cell Array (link)
Ok cool! thanks
I have tried this
syms a
for i=1:numFiles
for j=1:length(Ind{i})
Sol{i}(j) = solve((a/2)*log((Rs+a)/(Rs-a))== Ind{i}(j),a);
end
end
But it does not work... Then I have tried
for i=1:numFiles
for j=1:length(Ind{i})
Sol{j,i} = solve((a/2)*log((Rs+a)/(Rs-a))== Ind{i}(j),a);
end
end
And more or less works (I get the solutions for all set of data) although it gives me more cells than I wanted since I just want sol = cell(1,10). What am I missing?
Thank you!
My pleasure.
If you only want ‘Sol{1,10}’, assign it to another variable and use that in the rest of your calculations:
Result = Sol{1,10};
You might also find the ‘matlabFunction’ function helpful if you want to convert ‘Result’ into an anonymous function. You can then use it to provide numeric results in the rest of your code.

Sign in to comment.

More Answers (0)

Categories

Find more on Operators and Elementary Operations in Help Center and File Exchange

Asked:

on 14 Mar 2018

Commented:

on 19 Mar 2018

Community Treasure Hunt

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

Start Hunting!