Finding corresponding values in double arrays in structure fields
Mostrar comentarios más antiguos
I have two structures in a for loop with i = 1:n
A(i).a
B(i).b
For each loop i, the field a of structure A contains a double array with r(i) rows and 1 column, and the field b of structure B contains a double array with r(i) rows and m columns. In other words, the number of rows of the double array in the field of each structure changes with i and it is the same for both structures and equal to r(i). Also, the number of columns in field a of structure A is fixed and equal to 1, and the number of columns in field b of structure B is fixed and equal to m.
For each loop i, I want to find out the column index for each row of the double array contained in field b of structure B of the element which has a value equal to the value of the element on the same row of the double array contained in field a of structure A. For each loop i, the result should be a double array with 1 column and a number of rows equal to the number of rows of the double array in each field. I want to store the result in a structure R(i).r.
I tried the following, but it only works when the number of rows of the double arrays is equal to 1. When the number of rows of the double arrays is >1, I get all kind of wrong numbers!
R(i).r = find(B(i).b(:,:) == A(i).a(:));
Thank you very much!
5 comentarios
Jan
el 15 de Mayo de 2019
This is not clear:
"find out the column index for each row of the double array contained in field b of structure B of the element which has a value equal to the value of the element on the same row of the double array contained in field a of structure A"
Which "column index"?
Please post an example to clarify what you expect and what "I get all kind of wrong numbers" means.
Jan
el 15 de Mayo de 2019
Thanks. Based on your previous suggestion, I tried the following
R(i).r = ismember(B(i).b(:,:) , A(i).a(:))
and it works, but I get a logical matrix with 1 in the row and column position where the elements of the two double arrays in the field of the structures are equal.
How do I get now from
0 0 0 0 1 0 0
0 0 1 0 0 0 0
the indexes
5,3?
Thanks
Jan
el 15 de Mayo de 2019
I still think it would be more efficient, if you post some inputs and teh wanted outputs.
I assume you can simply your code to
R(i).r = ismember(B(i).b , A(i).a)
Try this:
m = [0 0 0 0 1 0 0; ...
0 0 1 0 0 0 0];
m * (1:size(m, 2)).'
Stephen23
el 16 de Mayo de 2019
Jan,
thanks a lot for your assistance. I have attached the two structures A.a and B.b. If I use the code below
clear;
load AB
for n = 1:81;
R(n).r = find(A(n).a == B(n).b);
end
I get the structure R.r attached. You can note that "the code works" when the double array in B(n).b has only one row. For example, for n = 40, or n = 65 or n = 73. When the double array in B(n).b has more than one row, for example for n = 45 or n = 47 or n = 74, I get some meaningless numbers.
To be clear: "the code works" means that the codes correctly finds the column of the value in the row of the double array in B.b that is equal to the corresponding row value of the double array in A.a.
To be even more clear here is an example. For n = 40, the code does find in which column of the single row double array contained in B(40).b the value A(40).a = 16.8967 is. It does find that it is in column 14.
The code does not work for n = 45, in which case the double array A(45).a has 7 rows and R(45).r = [39;48;49;120;122;142;152] which is meaningless considering that the maximum number of columns of the double array contained in B(45).b is 22.
Thank you!
" I get some meaningless numbers."
"which is meaningless considering that the maximum number of columns of the double array..."
They are certainly not meaningless. As its documentation clearly states, with only one output argument find returns the linear indices (not column or row indices as you seem to assume).
"...the value A(40).a = 16.8967..."
Checking for equality of such values is very likely to fail due to floating point error issues. You should be using ismembertol or comparing the absolute difference against a tolerance.
As an aside, what is the point in using structure with just one field? Why not use a simpler container (e.g. a cell array)?
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Structures en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!