From: Andy on
> Problem here is that YtransMatrix_i are really 4 2x2 matrices full of data. The names YtransMatrix_0 to YtransMatrix_3 come from 4 devices named from 0 to 3.
> Then I want to construct another matrix Mx whose elements are functions of the elements of the YtransMatrix [in the example I showed you the first column but it will be 4x3 matrix with also coupled elements of different YtransMatrix]

If you have 4 matrices each 2x2, then the correct way to store them is in a 2x2x4 array, YtransMatrix. Then YtransMatrix(:,:,1) would be what you now call YtransMatrix_0; YtransMatrix(:,:,2) would be what you now call YtransMatrix_1; etc. You can then solve your original problem as follows:


YtransMatrix = reshape(1:16,[2 2 4]); % some sample data

%{
% the matrix displayed
YtransMatrix(:,:,1) =

1 3
2 4

YtransMatrix(:,:,2) =

5 7
6 8

YtransMatrix(:,:,3) =

9 11
10 12

YtransMatrix(:,:,4) =

13 15
14 16

% your desired output would be the (1,1) element of each of these slices squared
% hence [1 25 81 169]
%}

Mx(1:4) = squeeze(YtransMatrix(1,1,:).^2);

% see help squeeze for more on squeeze
From: Javier on
"Andy " <myfakeemailaddress(a)gmail.com> wrote in message <i1n0ue$hj4$1(a)fred.mathworks.com>...
> > Problem here is that YtransMatrix_i are really 4 2x2 matrices full of data. The names YtransMatrix_0 to YtransMatrix_3 come from 4 devices named from 0 to 3.
> > Then I want to construct another matrix Mx whose elements are functions of the elements of the YtransMatrix [in the example I showed you the first column but it will be 4x3 matrix with also coupled elements of different YtransMatrix]
>
> If you have 4 matrices each 2x2, then the correct way to store them is in a 2x2x4 array, YtransMatrix. Then YtransMatrix(:,:,1) would be what you now call YtransMatrix_0; YtransMatrix(:,:,2) would be what you now call YtransMatrix_1; etc. You can then solve your original problem as follows:
>
>
> YtransMatrix = reshape(1:16,[2 2 4]); % some sample data
>
> %{
> % the matrix displayed
> YtransMatrix(:,:,1) =
>
> 1 3
> 2 4
>
> YtransMatrix(:,:,2) =
>
> 5 7
> 6 8
>
> YtransMatrix(:,:,3) =
>
> 9 11
> 10 12
>
> YtransMatrix(:,:,4) =
>
> 13 15
> 14 16
>
> % your desired output would be the (1,1) element of each of these slices squared
> % hence [1 25 81 169]
> %}
>
> Mx(1:4) = squeeze(YtransMatrix(1,1,:).^2);
>
> % see help squeeze for more on squeeze



Thank you very much! Now it's clear :)
From: Steven Lord on

"Javier " <jagon(a)postal.uv.es> wrote in message
news:i1mltp$a1v$1(a)fred.mathworks.com...
> Hi guys,
> a real basic question I guess. But I didn't had a course on that and I'm
> learning on my own.
>
> I have four matrices, whose (1,1) elements are, say:
>
> YtransMatrix_0(1,1) = 0;
> YtransMatrix_1(1,1) = 1;
> YtransMatrix_2(1,1) = 2;
> YtransMatrix_3(1,1) = 3;
>
> I want to create one matrix with the first column being the elements
> before to the second power. I would like to do it in a quick form taking
> advantage of the index in the name of the four matrices YtransMatrix_i
>
> I tried something like:
>
> Mx(1:4,1)=pow(sprintf('YtransMatrix_%d(1,1)',0:3),2);
>
> But that's not working. I don't know how to manage an index in the
> variable name.

Others have given alternate approaches (a 3-D array being one; a cell array
would be another.) If you're curious why people are so insistent that you
move away from this type of naming scheme, take a look at question 4.6 in
the newsgroup FAQ (linked in my signature.)

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com


From: Javier on
"Steven Lord" <slord(a)mathworks.com> wrote in message <i1n33v$8es$1(a)fred.mathworks.com>...
>
> "Javier " <jagon(a)postal.uv.es> wrote in message
> news:i1mltp$a1v$1(a)fred.mathworks.com...
> > Hi guys,
> > a real basic question I guess. But I didn't had a course on that and I'm
> > learning on my own.
> >
> > I have four matrices, whose (1,1) elements are, say:
> >
> > YtransMatrix_0(1,1) = 0;
> > YtransMatrix_1(1,1) = 1;
> > YtransMatrix_2(1,1) = 2;
> > YtransMatrix_3(1,1) = 3;
> >
> > I want to create one matrix with the first column being the elements
> > before to the second power. I would like to do it in a quick form taking
> > advantage of the index in the name of the four matrices YtransMatrix_i
> >
> > I tried something like:
> >
> > Mx(1:4,1)=pow(sprintf('YtransMatrix_%d(1,1)',0:3),2);
> >
> > But that's not working. I don't know how to manage an index in the
> > variable name.
>
> Others have given alternate approaches (a 3-D array being one; a cell array
> would be another.) If you're curious why people are so insistent that you
> move away from this type of naming scheme, take a look at question 4.6 in
> the newsgroup FAQ (linked in my signature.)
>
> --
> Steve Lord
> slord(a)mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com
>


Thak you very much. I'll give a look at this.

Just another thing about preallocating.
I made it like this because I had a warning saying that preallocating would be faster than a for loop:
My(1:4,3)=pow(YtransfMatrix(1,2,1:4),2);
and it should work.

But, how to make this when I have something like:
For i=1:4
Mxy(i,2)=XtransfMatrix(1,1,i)*YtransfMatrix(1,2,i);
end
He says to me that I should preallocate for speed. But I cannot make in the way like before: Mxy(1:4,2)=XtransfMatrix(1,1,1:4)*YtransfMatrix(1,2,1:4); Obviously second part of equation is 4 by 4, not 4 by 1.
From: Andy on
Since you appear to want to multiply element by element, you should use the .* operator instead of just the * operator.