Modify cell array in multidimensional array depending on number

I have a cell array (part of a multidimensional array) going from 0 to 1 in uneven steps. I wish to perform different calculations on this array, depending on if the values are less than, or greater than 0.5. I was trying to achieve this with an if statement, but I'm not sure if this is the correct approach.
for i=1:2
Timestep{i,1} = Radial{i,1}(:,1);
if Timestep{i,1}<0.5
CSA{i,1}(k) = OriginalDiameter-RadialDisp;
k=k+1
end
if Timestep{i,1}>0.5
CSA{i,1}(k) = CrimpedDiameter+RadialDisp;
k=k+1
end
end

3 个评论

Why use cell arrays and not simple numeric arrays? Life would be much easier.
Ciara's "Answer" moved here:
I'm reading in data from columns in a txt file, so this is why I assign in a multidimensional array
@Ciara: it is not clear why you need the extra complexity of using cell arrays. Most likely using ordinary numeric arrays would make your code much simpler.

请先登录,再进行评论。

回答(1 个)

What should happen for Timestep{i,1}==0.5?
Working with numerical arrays would be easier. Work with cells only, if the elements have different sizes or types. Otherwise (maybe multi-dimensional) numerical arrays are more efficient and convenient. That the data are imported from a file is no good argument for cells.
If your code is correct or not depends on the meaning of k and on what you want as output. The code itself looks valid.

类别

在 帮助中心 和 File Exchange 中查找有关 Multidimensional Arrays 的更多信息

标签

提问:

2017-12-19

评论:

2017-12-20

Community Treasure Hunt

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

Start Hunting!

Translated by