Create & Calling list of Matrix name

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Create & Calling list of Matrix name

quocphantruong
Hello
My problem is about naming and calling set of matrix name using loop
1. Let say I have a large matrix X(100x2). Now I want to cut this matrix
into 20 sub-matrixes and naming them from A1 to A20, each has size(5x2)

2. Then I need to perform some operation hence, need to call out matrix Ai
(for i=1:20) then how should I call them out in loop.

I search and there is some solution:
  for i = 1:20
    eval (sprintf ("A%d = %d;", i, i));
  endfor

Howevever, this seems does not work with matrix. And I dont get how the code
is doing, if you could help me explain that would be great
THank you
Phan




--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: Create & Calling list of Matrix name

Sergei Steshenko

On 3/2/19 12:39 AM, quocphantruong wrote:

> Hello
> My problem is about naming and calling set of matrix name using loop
> 1. Let say I have a large matrix X(100x2). Now I want to cut this matrix
> into 20 sub-matrixes and naming them from A1 to A20, each has size(5x2)
>
> 2. Then I need to perform some operation hence, need to call out matrix Ai
> (for i=1:20) then how should I call them out in loop.
>
> I search and there is some solution:
>    for i = 1:20
>      eval (sprintf ("A%d = %d;", i, i));
>    endfor
>
> Howevever, this seems does not work with matrix. And I dont get how the code
> is doing, if you could help me explain that would be great
> THank you
> Phan
>
>
>
>
> --
> Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html
>
>

You might want your each small matrix to become a cell in a cell array (
https://octave.org/doc/v4.2.0/Cell-Arrays.html ), and then you will use
index of the cell in cell array to access your small matrices in your loop.


--Sergei.



Reply | Threaded
Open this post in threaded view
|

Re: Create & Calling list of Matrix name

apjanke-floss
In reply to this post by quocphantruong


On 3/1/19 5:39 PM, quocphantruong wrote:

> Hello
> My problem is about naming and calling set of matrix name using loop
> 1. Let say I have a large matrix X(100x2). Now I want to cut this matrix
> into 20 sub-matrixes and naming them from A1 to A20, each has size(5x2)
>
> 2. Then I need to perform some operation hence, need to call out matrix Ai
> (for i=1:20) then how should I call them out in loop.
>
> I search and there is some solution:
>    for i = 1:20
>      eval (sprintf ("A%d = %d;", i, i));
>    endfor
>
> Howevever, this seems does not work with matrix. And I dont get how the code
> is doing, if you could help me explain that would be great
> THank you
> Phan
>

Working with multiple named variables using sprintf() and eval() is only
going to bring you unhappiness. Instead of using 20 variables named A1,
A2, ... A20, try using a single 20-long cell array named A, which you
can index as A{1}, A{2},  ... A{20}.

For example:

X = meshgrid (1:2, 1:100);
n = 20;
A = mat2cell(X, repmat(n, [size(X,1)/n 1]), 2);

Now you can loop over them and do things like:

B = cell (size (A));
for i = 1:numel (A)
    B{i} = some_operation (A{i});
endfor

Or, more concisely, use cellfun:

B = cellfun (@(x) {some_operation (x)}, A);

Cheers,
Andrew


Reply | Threaded
Open this post in threaded view
|

Re: Create & Calling list of Matrix name

quocphantruong
Oh I get it
Thank you so much

So this mean, I simply need to switch between cell2mat then mat2cell for
operation right?




--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html