Managing Matrix Content Using Loop ?

e.g i have variable data that containt matrix like these ;
a1 b1 c1
a2 b2 c2
a3 b3 c3
and i wanted it stored like :
a1
a2
a3
b1
b2
b3
c1
c2
c3
I use looping
inc=1;
[s1,s2]=size(data);
for x=1:s2
for y=1:s1
dataStored(inc)=data(y,x);
inc=inc+1;
end
end
It's work, but on a very large amount of data like data < 512x3671 int 16 > take endless time to wait. Is there any way simple to do that? Or maybe my code is wrong ?

 Respuesta aceptada

Cedric
Cedric el 24 de Mzo. de 2013
Editada: Cedric el 24 de Mzo. de 2013
>> A = magic(3) % Just an example..
A =
8 1 6
3 5 7
4 9 2
>> v = A(:) % Read A as a column vector (linear indexing).
v =
8
3
4
1
5
9
6
7
2

6 comentarios

I Made
I Made el 25 de Mzo. de 2013
Editada: I Made el 25 de Mzo. de 2013
Thanks, it can be simple like that. It's really sweet, can you give me link to do trick-trick in matlab ? if i have 2 variable in matrix, eg i have a
a1
a2
a3
and variable b
b1
b2
b3
i wanted to store it in c like
a1 b1
a2 b2
a3 b3
i need some tutorial to get over with matrix management issue.. really
Cedric
Cedric el 25 de Mzo. de 2013
Google "MATLAB tutorial" or pick one here:
e.g. the Getting Started 2MB PDF.
What we are using in my solution is linear indexing. There aren't that many types of indexing: subscribed, linear, logical.
For your second question, you want to read the doc about CAT, HORZCAT, VERTCAT. You can also do it this way:
a = [6, 7, 8].'
b = [1, 2, 3].'
c = [a,b]
This outputs
a =
6
7
8
b =
1
2
3
c =
6 1
7 2
8 3
I Made
I Made el 25 de Mzo. de 2013
How if b variable is less than a variable like
a1
a2
a3
but then b only
b1
b2
this will make Subscripted assignment dimension mismatch. How do i put it like
a1 b1
a2 b2
a3 0
Cedric
Cedric el 25 de Mzo. de 2013
You can only CAT arrays on dimensions with same length. If you are not in this situation, you have fist to adapt lengths. If b had been a 2x1 vector in the previous case, I would have had to add some padding, e.g. 0, to match lengths:
>> [a, [b;0]]
ans =
6 1
7 2
8 0
I Made
I Made el 25 de Mzo. de 2013
i tried it but get another error : CAT arguments dimensions are not consistent...
which my variable : a < 1879552x1 int16 > and b < 1879402x1 int16 > then i do :
c = [a, [b;0]]
Cedric
Cedric el 25 de Mzo. de 2013
Editada: Cedric el 25 de Mzo. de 2013
Well, [b;0] adds only one 0 element to b and look at the size of each of your vectors.. the size difference is much greater than just one element. It is 150 actually. Now if it is really meaningful to add 150 0's to b, you can do it as follows:
bext = [b; zeros(150,1)] ;
c = [a, bext] ;
but as far as I'm concerned, it would be a good investment of your time to read a tutorial, so you understand at least the basics about array manipulation in MATLAB.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Preguntada:

el 24 de Mzo. de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by