matrix of matrices - matrix with matrices inside it

조회 수: 405 (최근 30일)
Daniel Brower
Daniel Brower 2019년 12월 11일
댓글: Mandar Kadwekar 2022년 6월 5일
How would I create a matrix of matrices, such that A = [[1 2 3 4] [5 6 7 8]]?
I mean a matrix with matrices inside of it.

답변 (2개)

JESUS DAVID ARIZA ROYETH
JESUS DAVID ARIZA ROYETH 2019년 12월 11일
편집: JESUS DAVID ARIZA ROYETH 2019년 12월 11일
use cell array:
A = {[1 2 3 4] [5 6 7 8]}
A{1}% get matrix 1
A{2}% get matrix 2
  댓글 수: 7
Image Analyst
Image Analyst 2022년 6월 5일
Yes, cellfun can be cryptic.
Does this help:
% Define sample data:
A = {[1 2 3 4] [5 6 7 8]} % A 1 by 2 cell array with each cell containing a matrix that is a 1-by-4 row vector.
A = 1×2 cell array
{[1 2 3 4]} {[5 6 7 8]}
% Define an anonymous function:
functionHandle = @(inputArg) inputArg(2) % Return second element of whatever inputArg is passed in.
functionHandle = function_handle with value:
@(inputArg)inputArg(2)
% Apply the function to each cell of A.
% This will take each cell of A and take the matrix out, and then pass it to functionHandle
% which will take the second element of that matrix and return it.
results = cellfun(functionHandle, A)
results = 1×2
2 6
Mandar Kadwekar
Mandar Kadwekar 2022년 6월 5일
Yes this exactly did what I intended to do with the matrix. Thanks.

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2019년 12월 11일
Some more ways
A = [1 2 3 4; 5 6 7 8] % Create a 2-D array with the vectors in it.
A = cat(2, [1 2 3 4], [5 6 7 8]); % Create a 2-D array with the vectors in it.
For more on cell arrays, like in Jesus's answer, see The FAQ. One advantage of cell arrays is that each cell can contain anything : matrices of all the same or all different sizes, string, tables, even other cell arrays. Any MATLAB variable can be put into the cell and each cell can be totally different.
Another variable that can contain matrices is a table, though I think each column in a table must contain the same size array.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by