How to create a Matrice from a structure

1 visualización (últimos 30 días)
Hidir ABAY
Hidir ABAY el 20 de Mayo de 2016
Editada: Walter Roberson el 31 de Mayo de 2016
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 comentarios
Rakesh Chavan
Rakesh Chavan el 26 de Mayo de 2016
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
Hidir ABAY
Hidir ABAY el 31 de Mayo de 2016
Hello, thank you for your answer and your advice.

Iniciar sesión para comentar.

Respuestas (2)

Guillaume
Guillaume el 27 de Mayo de 2016
"...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?
  1 comentario
Hidir ABAY
Hidir ABAY el 31 de Mayo de 2016
Hello, haha you were right, I did not need to work with matrix. I find the way to use my structure. Thanks for your answer.

Iniciar sesión para comentar.


Nut
Nut el 27 de Mayo de 2016
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
  1 comentario
Hidir ABAY
Hidir ABAY el 31 de Mayo de 2016
Thank you for your answer I hope it will help another one. I find a way to work with my structure and so I don't need to create a matrix anymore. Have a nice day.

Iniciar sesión para comentar.

Categorías

Más información sobre Just for fun 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!

Translated by