myradio
myradio

Reputation: 1775

Take the same subset from many matrices of the same size in MATLAB

Say I have many matrices a,b,c,d...z

They are all the same dimension,

>> size(a)

ans =

   M   N

Now, I want to get (assuming mod(M,2)=0 and mod(N,3)=0)

a_new = a(1:2:end,1:3:end);
b_new = b(1:2:end,1:3:end);
.
.
.
z_new = z(1:2:end,1:3:end);

Is there a way to do this easily?

Important note: I want to do this to all the elements in the current workspace of the size MxN, so, if there is a way to filter all the current variables of MxN and take the subset that would suffice.

Upvotes: 0

Views: 41

Answers (1)

flawr
flawr

Reputation: 11628

Dynamic variable names are a very, very, very, very bad idea.

If you really need to do something consider using cell arrays or other alternatives linked in the tutorial above.

If you still want to do this, consider following snippet:

list = who;
for k=1:length(list)
    if ismatrix(eval(list{k})) && all(size(eval(list{k})) == [M, N])
        eval([list{k},'_new = ',list{k},'(1:2:end,1:3:end);']);
    end
end

Upvotes: 2

Related Questions