Can I loop on a specific element in an array?

1 view (last 30 days)
James
James on 28 Jan 2011
I have a 4x1 matrix and I need to loop through changing one cell (3,1) through a set.
(I am new to Matlab/and program language-I am sure there is a simple to do this but the mathworks website and I could not agree on a answer)
conductivity = [#, #, c, #] (I need to change c)
Here is an example of my problem:
conductivity = [3.2; 0.05; 1; 0.05];
for conductivity(3) = 1:10
??? for conductivity(3) = 1:10
|
Error: Unbalanced or unexpected parenthesis or bracket.
Conductivity being used as a model parameter for a electromagnetic parametric study, with row 3 being a main parameter of investigation.
Would I go:
for c = 1,2,3,4,5 or 1:50
do my stuff....
or along the lines of:
for conductivity(3,1) = ........
  1 Comment
Todd Flanagan
Todd Flanagan on 28 Jan 2011
Oleg says, "If I understand it correctly, you want to change the 3rd value of conductivity several times (sensitivity analysis?). If that's the case you may want to post the code you want to execute inside the loop...and we may find a better way to test it for different values of conductivity(3)"

Sign in to comment.

Answers (2)

Oleg Komarov
Oleg Komarov on 28 Jan 2011
You should be carefull with terminology here. A cell is a fundamentally different class if compared to a double matrix. The way you access cell content is different.
Then assuming you have a 4 by 1 double matrix, if you want to change it's content do simply
mymatrix(3) = 10;
If you have a cell array, then:
myC{3} = 10;
Finally, what do you need a loop for and could you be more precise on what are you trying to do and maybe post some of the code you tried?
Oleg

Andrew Newell
Andrew Newell on 28 Jan 2011
I assume that by "cell" you mean "component of a vector". There are a lot of ways to do this, but one option would be
conductivity = [a,b,0,d];
for c=1:50
conductivity(3) = c;
do_stuff_with_it(conductivity);
end
  4 Comments
Ruben Verkempynck
Ruben Verkempynck on 18 May 2011
Hey guys,
i'm guessing
do_stuff_with_it()
is the function that uses the parameter that you want to vary?
I want to do the same actually.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!