How to create a Matrice from a structure
1 回表示 (過去 30 日間)
Hello everyone, first sorry for my poor english ^^
I ask for your help today because I have a little problem.
I have created a structure like this :
So, here I have a structure named 'values' that contains intensity values for each pixel of each 'region' or 'object' L. Some region contains 5 pixels, some others only 1 so and so..
So I have a structure like this, for exemple: values
I want to create a matrice with this structure and I dont know how to do it.
In my program I have already created a matrice from a structure. My structure is
and it works well.
Why does it work for this one ? Because meanValues is a structure that contains only one value for each region. So there are any probleme to create a matrice from this.
How can I create a matrice for my strucuture named 'values' ?? I don't know..
Thanks for reading, and for youre future answers :) !
回答 (2 件)
Guillaume 2016 年 5 月 27 日
"...and it works well." and is also overly complicated:
meanintensity = [meanValues.meanIntensity];
will be a lot faster and is the proper way to convert scalar fields of a structure array into a matrix.
The answer to your question depends on which fields of the regionprops structure you want to convert to a matrix. Unfortunately, you haven't told us that. However, if as you say it's one of these fields with a various number of element, how are you expecting to have that into a single matrix?
Maybe you want to concatenate it all into one vector:
allPixelIdxList = vertcat(values.PixelIdxList);
Note that the above will only work if the field you want to concatenate is always a column vector.
But in the end, why do you want to convert the output into a matrix? Why can't you work with the structure as it is?
Nut 2016 年 5 月 27 日
if "meanValues" is a single value and you return a 2d matrix from it, I think that if "values" is an array you should return a 3d matrix. I don't know the right command to do this... but if you can accept a not much efficient solution, you can try to apply the same syntax you have already created. Example (this is not a right syntax, it is just to explain the solution, you have to adjust it):
matrice = zeros(dim1,dim2,number_of_values); % Preallocation of "matrice"
for i = 1:number_of_values
matrice(:,:,i) = matrice=cell2mat(struc2cell(values(i)))