MATLAB Answers

Obtain specific dimension from multidimensional Arrays

4 views (last 30 days)
Ali Tawfik
Ali Tawfik on 12 May 2020
Answered: Stephen Cobeldick on 13 May 2020
Hi All,
I am trying to create a new variables based on multidimensional array obtained, for the simplicity I just got the part of the code, I wanna do.
I just want to obtained only the dimension with different values :
For example: s(:,:,1), s(:,:,3), s(:,:,5).. the point is sometimes these values change so I would like to do it with a for a loop or whatever so whatever the dimension number .. I just want the new values with only the different numbers!!
Any help!!
clear all;
clc;
s(:,:,1)=[10;20;30];
s(:,:,2)=[10;20;30];
s(:,:,3)=[100;200;300];
s(:,:,4)=[10;20;30];
s(:,:,5)=[50;60;70];
s(:,:,6)=[10;20;30];
for i=1:2
s_new(:,:,i)=s(:,:,i)
end

  0 Comments

Sign in to comment.

Answers (3)

James Tursa
James Tursa on 12 May 2020
Not really sure what you are asking, but if you only want to pick off some indexes from an arbitrary dimension, simply supply them as a range or vector of indexes. E.g.,
s_new = s(:,:,1:2)
or
s_new = s(:,:,[1 2])

  1 Comment

Ali Tawfik
Ali Tawfik on 12 May 2020
Thanks for your prompt reply.
But I am talking if I would do this automatically ?
I mean If i have an arbitrary dimension, so can not I just pick ooff the indexes which have different number ?

Sign in to comment.


Stephen Cobeldick
Stephen Cobeldick on 12 May 2020
Use a cell array of the appropriate size to define the indices. Then you can refer to any arbitrary dimension using indexing, e.g. the 3rd dimension of a 5D array:
A = rand(1,2,3,4,5); % 5D
C = repmat({':'},1,5);
C{3} = [1,2,3]; % 3rd dimension, any indices you want.
A(C{:})

  1 Comment

Ali Tawfik
Ali Tawfik on 12 May 2020
@stephen Codeldick
Thanks for your answer... Seems interesting, and could help!
But I am not sure if what I am asking could be done or not?
Your asnwer working fine, but I am just wondering is there a way to do it to get or refer to the indices which are NOT equal like the following:
clear all;
clc;
A(:,:,1) = [10;20]; % WANT THIS
A(:,:,2)=[1;2] % WANT THIS
A(:,:,3)=[1;2]
A(:,:,4)=[100;200] % WANT THIS
A(:,:,5)=[1;2]
C = repmat({':'},1,3);
C{3} = [1,2,4]; % BUT WHAT IF THE A CHANGES , I WANT here the operation to be automatic
z=A(C{:})

Sign in to comment.


Stephen Cobeldick
Stephen Cobeldick on 13 May 2020
You can just use basic subscript indexing:
A(:,:,1) = [10;20]; % WANT THIS
A(:,:,2)=[1;2] % WANT THIS
A(:,:,3)=[1;2]
A(:,:,4)=[100;200] % WANT THIS
A(:,:,5)=[1;2]
X = [1,2,4];
Z = A(:,:,X)

  0 Comments

Sign in to comment.


Translated by