Multidimensional matrix to use for loop

I have a multidimensional matrix which is X.
when i type X in command window the answer is below.
X(:,:,1)
10 5 2
5 3 1
X(:,:,2)
8 4 1
7 -5 -10
X(:,:,3)
18 15 7
12 11 9
ı want to write a code using for loop to calculate some values form this matrix.
i mean
for i=1:3
a(i)=X(i)
end
something like this.
I want to get matrices as
a(1)=X(:,:,1)
a(2)=X(:,:,2)
a(3)=X(:,:,3)
How can i get it?

2 Comments

Already your matrix X is in theat format only.
a{1} = X(:,:,1) ;
Hi friend
That not worked,
Can you take a look my comment below

Sign in to comment.

Answers (1)

Stephen23
Stephen23 on 21 Mar 2019
Edited: Stephen23 on 22 Mar 2019
for k = 1:3
X(:,:,k)
end
If you want to split into a cell array:
a = cell(1,3);
for k = 1:3
a{k} = X(:,:,k)
end
Or use num2cell or mat2cell. Also read this:

12 Comments

Gko K
Gko K on 21 Mar 2019
Edited: Stephen23 on 22 Mar 2019
Hi friend,
That not worked
It says,
a =
1×3 cell array
{10×3 double} {0×0 double} {0×0 double}
a =
1×3 cell array
{10×3 double} {10×3 double} {0×0 double}
a =
1×3 cell array
{10×3 double} {10×3 double} {10×3 double}
here is my whole code.
I want to get every X matrix and i will have an operation on these matrix.
[stry stryname]=xlsread('data.xlsx','Story');
[num str]=xlsread('data.xlsx','Shear');
stry(end,:)=[];
stry=stry(:,2);
stry=sortrows(stry);
str=str(:,[1 2 3 4]);
num=num(:,2);
str(1,:)=[];
strdel=strcmp(str(:,4),'Top');
str(strdel,:)=[];
num(strdel,:)=[];
strydup=length(unique(str(:,2)))*length(unique(str(:,3)));
stry=repmat(stry,strydup,1);
[~,a] = sortrows(str(:,2:4));
outstr = str(a,:);
outnum = num(a,:);
outstr(:,1) = erase(outstr(:,1),"STORY");
kat = outstr(:,1);
kat = cellfun(@str2num,kat);
veri=[kat outnum stry];
xind=1;
begind=1;
lastind=1;
lngt=length(veri);
for i=1:lngt
pier=outstr(i,2);
load=outstr(i,3);
loc=outstr(i,4);
if i<lngt
pier2=outstr(i+1,2);
load2=outstr(i+1,3);
loc2=outstr(i+1,4);
if ~ (isequal(pier,pier2) && isequal(load,load2) && isequal(loc,loc2))
begind;
lastind=i;
X(:,:,xind)=veri(begind:lastind, 1:3);
vd(:,:,xind)=veri(begind:lastind, 2);
xind=xind+1;
begind=i+1;
lastind=i+1;
end
else
begind;
lastind=i;
xind=xind;
X(:,:,xind)=veri(begind:lastind, 1:3)
vd(:,:,xind)=veri(begind:lastind, 2)
end
end
Hi Stephen,
Did you edit your comment?
I cant find your new comment.
"That not worked"
The displayed cell array that you show indicates that it worked correctly. You can access the contents of the cell array using indexing:
Your code has a totally unrelated variable a, so it is not clear how your code is related to your question or to my answer.
I am a beginner in matlab, so i can be wrong about my question.
Thank for your help friend.
I want to get every X(:,:,i)in my code above to use in for loop. But i cant get the right results.
"I want to get every X(:,:,i)in my code above to use in for loop."_
Sure, my answer shows you how to do that.
"But i cant get the right results."
As you have not explained what the "right results" are, we have no idea what you expect to get that cannot be achieved using the code from my answer.
Thanks friend,
I upload a screenshot here , and i showed there what i wan to get.
01.jpg
Stephen23
Stephen23 on 22 Mar 2019
Edited: Stephen23 on 22 Mar 2019
Sure, it gave an error. But the code you tried is not what I showed you in my answer.
You will probably have to use a cell array, just like I showed you in my answer. In your code you are trying to assign multiple numeric array elements to a different number of numeric array elements, which will never work.
If you really need to assign to a numeric array then you will have to pay careful attention to the number of elements that you are assigning, e.g.
  1. padding the RHS with zeros/NaN/..., and/or
  2. using indexing on the LHS.
Thank you again.
But i am really a beginner, so i think i cant achieve the operation.
I am confused but thank you
"But i am really a beginner, so i think i cant achieve the operation."
You can use a cell array, just use the code in my answer.
What you are trying doesn't work.
I really thank you but i cant do what i want.
Can you try your opinion on my code above.
I want get every X(:,:,i) and find their absolute max value in column 2 with using for loop or another way.
At the end of my code i have a X matrix (10x3x6 double)
I want to get X(:,:,1), X(:,:,2), X(:,:,3), X(:,:,4), X(:,:,5), X(:,:,6) as matrices a1, a2, a3, a4, a5, a6.
Stephen23
Stephen23 on 24 Mar 2019
Edited: Stephen23 on 24 Mar 2019
"Can you try your opinion on my code above."
"I want get every X(:,:,i) and find their absolute max value in column 2 with using for loop or another way."
Personally I would get rid of the loop entirely and simply use indexing and max with its optional dimension input:
>> A = randi(99,4,3,2)
A =
ans(:,:,1) =
80 61 61
35 63 16
44 9 77
40 30 56
ans(:,:,2) =
78 76 34
15 42 21
9 38 98
57 87 79
>> squeeze(max(abs(A(:,2,:)),[],1))
ans =
63
87
"I want to get X(:,:,1), X(:,:,2), X(:,:,3), X(:,:,4), X(:,:,5), X(:,:,6) as matrices a1, a2, a3, a4, a5, a6."
You might want that, but that is a really bad way to write code. Read this to know why:
If you really want to use a loop then use a cell array, exactly as I showed you in my answer.
Ok thank you,
I am not a computer engineer or software developer. So its hard for me to think and learn as a computer engineer. Sorry if i ask so many simple or complex question.
I will try to think and solve with your opinion.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Release

R2018b

Asked:

on 21 Mar 2019

Commented:

on 25 Mar 2019

Community Treasure Hunt

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

Start Hunting!