필터 지우기
필터 지우기

Matrix storage and extraction inside a cell or a strucutre array.

조회 수: 8 (최근 30일)
Ron Herman
Ron Herman 2020년 5월 3일
댓글: Ameer Hamza 2020년 5월 4일
How do I store matrix and extract matrix from a cell or a structure by using a for loop??????
a=[1 2 3;4 5 6;7 8 9]
b=[11 12 13;14 15 16;7 8 19]
c=[121 212 213;14 15 16;27 28 29]
I created an cell array by following method
d=[{a},{b},{c}]
Suppose I have to retrive b from d. How do I retrive it????
Suppose a,b,c are images represented in matrix form. I need to know if these matrix can be stored in a structure or in a cell array by using a for loop and later retrived by a for loop.

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 5월 3일
편집: Ameer Hamza 2020년 5월 3일
You can use curly bracket indexing with cell arrays. To extract the content of the second element from cell array 'd', run
b = d{2};
Also, note that you should never create a related variable with different names. So instead of creating a, b, c, and then using for loop to create cell array or struct. It is better to create the cell array to being with. There are ways to create cell array using for-loop using eval(), but it is highly unrecommended in MATLAB. See the reason here: https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval
  댓글 수: 2
Ron Herman
Ron Herman 2020년 5월 4일
Thank you Stephen and Ameer for the reply . It helped me.
I have some other doubts too.
Cant we create a blank structure or an empty structure???
Then store data into it and later use indexing to retrive data???
Ameer Hamza
Ameer Hamza 2020년 5월 4일
Yes you can do that. For example
s = struct(); % empty struct
s(1).a = rand(1,10); % create field a in struct
s(2).b = rand(2,20); % create field b and assign value to second
% element in struct array
However, all the elements in the struct array will have the same field names. If you don't assign a value to a specific field of an element of struct array, then It will be an empty vector. Run the above code to see the value of fields 'a' and 'b' for both elements of struct array 's'.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by