How do I divide a 3X3 matrix in three vertical or horizontal one-dimensional vectors using a FUNCTION?
    8 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
Just wanted to know how do I create a function which accepts a 3X3 matrix and returns three declared arrays from the vectors elements in the rows and columns. Can anyone help? (MATLAB beginner btw)
2 comentarios
  Stephen23
      
      
 el 1 de Feb. de 2016
				
      Editada: Stephen23
      
      
 el 1 de Feb. de 2016
  
			"from the vectors elements" from what vectors? You wrote that that input is a 3x3 matrix, and did not mention any vectors.
Please give us sample input and output arrays, so that we do not have to guess what you are trying to achieve.
Respuestas (2)
  Stephen23
      
      
 el 1 de Feb. de 2016
        
      Editada: Stephen23
      
      
 el 1 de Feb. de 2016
  
      Here are some ideas for you, using this sample matrix:
>> X = reshape(1:9,3,3)
X =
     1     4     7
     2     5     8
     3     6     9
Solution 1: Don't Split Your Data
The first solution is the easiest and the most recommended: don't split up your array. Why is it required to split it up? Although beginners seem to love splitting up their data, there really is no point: it just clutters up your workspace, and duplicates data that you already have. If you need to access parts of an array, then use simple, efficient indexing:
sum(X(1,:)) % sum the first row
This is a neat and efficient way to program in MATLAB. The general rule: keep your data together as much as possible, and use indexing to access it. This is the best solution.
Solution 2: Split Using Indexing
>> A = X(1,:)
A =
     1     4     7
>> B = X(2,:)
B =
     2     5     8
>> C = X(3,:)
C =
     3     6     9
Solution 3: Split Using num2cell
>> C = num2cell(X,1); % columns
>> C{:}
ans =
     1
     2
     3
ans =
     4
     5
     6
ans =
     7
     8
     9
>> R = num2cell(X,2); % rows
>> R{:}
ans =
     1     4     7
ans =
     2     5     8
ans =
     3     6     9
Note that num2cell puts the output vectors into a cell array, which is a convenient container for other arrays. I would recommend that you use this method (if you really must split up your data)
and deal
If you want to have three separate variables, then you could try deal:
>> [A,B,C] = deal(R{:})
A =
     1     4     7
B =
     2     5     8
C =
     3     6     9
but I would recommend that you keep them in the cell array.
No dynamic variables names
And whatever you do, do not try to create those variables dynamically. Do NOT create variable names dynamically! Here is why:
0 comentarios
  Guillaume
      
      
 el 1 de Feb. de 2016
        First and foremost, why? Why can't you keep using the 3x3 matrix and refer the rows / columns as appropriate? In all likelihood it will lead to simpler code. What's worrying about your question is that you seem to basically give variable names to individual columns or rows of a matrix. This is just a variation of the how-do-i-make-a-series-of-variables question and the answer to that is DON'T
If you're really intent on creating such a function, you can simply use indexing:
function [r1, r2, r3] = first3rows(m)
    r1 = m(1, :);  %change to :, 1 for columns
    r2 = m(2, :);  %change to : ,2 for columns
    r3 = m(3, :);  %change to :, 3 for columns 
end
A more advanced and generic version of the above:
function varargout = firstnrows(m)
   c = num2cell(m, 2)
   varargout  = c(1:nargout);
end
 which will return as many rows as output arguments defined by the caller.
By as said in my introduction, most likely, dont' do this.
0 comentarios
Ver también
Categorías
				Más información sobre Matrix Indexing en Help Center y File Exchange.
			
	Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

