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 :
values=regionprops(L,image,'PixelValues')
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
ans= 150
200
ans= 125
75
21
ans= 50
etc etc
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
meanValues=regionprops(L,image,'MeanIntesity')
and so,
matrice=cell2mat(struc2cell(meanValues))
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 个评论
Rakesh Chavan
2016-5-26
Hi,
Please correct me if my understanding of your question is wrong.
In the first case you have a variable sized arrays in your structure which can be converted to a cell but not to a matrix.
In the second case I think you only have fixed number of values in the structure which is why you are able to create a matrix.
For example:
s =
a: 1
b: 2
c: 2
>> cell2mat(struct2cell(s))
ans =
1
2
2
Whereas
s =
a: 1
b: 2
c: [2 9]
>> cell2mat(struct2cell(s))
Gives an error.
A sample script may be helpful in explaining the problem you are facing. This may assist others in providing a resolution to the query.
Hope this helps
-
Rakesh
回答(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
Hi,
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)))
end
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!