Can I loop on a specific element in an array?

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 个评论

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)"

请先登录,再进行评论。

回答(2 个)

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
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 个评论

James says, "Thank you both. Andrew hit the nail on the head on how to apply it for my reasons. This is what happens when a geologist tries to become a geophysicist"
As a geophysicist, I know that many geologists become excellent geophysicists, so don't get discouraged.
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.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Programming 的更多信息

产品

提问:

2011-1-28

Community Treasure Hunt

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

Start Hunting!

Translated by