For cycle to extract row of an Array and save them into vectors

3 次查看(过去 30 天)
Hello All, need some help with the following. I have an Array (4x4 cell), cal it A, and I need to create a "for" cycle that allows me to extract and save each row to a single vector. Ideally in this case the resulting vectors would be r1, r2, r3, r4. Sizes of all the vectors would of course be [1 4]. Thanks!
  3 个评论
Guillaume
Guillaume 2015-12-17
"it's hard not to want to go through the dynamic naming of variables route". Actually, I think it's much more work to got the dynamic route. Compare:
%with dynamic names
a = magic(4);
for row = 1 : 4
eval(sprintf('b%d = a(%d, :)', row, row)); %No automatic syntax check, have I made a typo in the string?
end
figure; hold on;
for row = 1 : 4
eval(sprintf('plot(b%d)', row)); %No automatic syntax check, have I made a typo in the string?
end
to:
%using cell arrays
a = magic(4);
b = num2cell(a, 2);
figure; hold on;
cellfun(@plot, b);
Stephen23
Stephen23 2015-12-17
编辑:Stephen23 2015-12-17
"damn it's hard not to want to go through the dynamic naming of variables route"
Consider that those numbers at the end of your numbered variable-names are basically indices... so then turn them into real indices that you can actually use. Instead of these dynamically named variables:
x1
x2
x3
etc
with all of their disadvantages: slow and insecure eval required; magic creation of variables into workspaces; totally unpredictable behavior until runtime; impossible to debug; lack of meaningful error messages; easily overwrites existing variables without warning; total loss of editor help tools, code hinting and variable highlighting. So many disadvantages... and yet we can simply turn them into real indices:
x{1}
x{2}
x{3}
etc
With indexing you get fast access using many indexing syntaxes. Easy to track variables. Easy to debug. Warning messages, hints and syntax messages in the Editor. So many tools that help us to code properly. Functions and operators that accept and process those arrays directly.
Many MATLAB functions operate on whole arrays (vectorization) or cell arrays (as Guillaume shows above), so why bother to make your own life harder by _removing the ability to use any of these functions directly.

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2015-12-15
编辑:Stephen23 2019-6-19

更多回答(1 个)

Adam
Adam 2015-12-15
编辑:Adam 2015-12-15
Do you want r1, r2, r3, r4 to be cell arrays too?
If so I don't see why you would ever want to do this when you could just index into the original.
Alternatively you could rework the original into a single cell array of 4 elements, each containing a [1 4] vector.
e.g.
A2 = cell( 4, 1 );
for n = 1:4
A2{i} = A(i,:);
end
though I'm sure there are neater methods to achieve that.
Don't create 4 numbered variables though, it is a bad idea. (And probably Stephen Cobeldick will be along soon with his usual informative post to save me the trouble of explaining why!)
  1 个评论
Guillaume
Guillaume 2015-12-15
"though I'm sure there are neater methods to achieve that" Indeed:
A2 = num2cell(A, 2); %works even if A is a cell array
I'm not convinced the original array is a cell array though. New posters are easily confused by plain matrices and cell arrays and tend to use the cell wording when referring to elements of plain arrays. It does not matter however, because the answer is the same regardless.
And yes, do not create numbered variables.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by