Filter löschen
Filter löschen

Bringing arrays to the same length

12 Ansichten (letzte 30 Tage)
Lev Mihailov
Lev Mihailov am 2 Mär. 2022
Bearbeitet: Rik am 2 Mär. 2022
There are three arrays (two vectors and a matrix), I need to bring them to the same length
a=[ 1.2,1.3,1.4]; % by array "a" i need to sort the values ​​in array "b" and "m"
b=[ 1.2,0.9,1.3,2.6,1.4];
m=[1,2;26,90;1,2;120,0;1,0];
% what do i need to get
ba=[ 1.2,1.3,1.4];
ma=[1,2;1,2;1,0];
I will be glad to any advice
  2 Kommentare
DGM
DGM am 2 Mär. 2022
Bearbeitet: DGM am 2 Mär. 2022
At first glance, I thought you're looking for the intersections:
a=[1.2,1.3,1.4]; % by array "a" i need to sort the values in array "b" and "m"
b=[1.2,0.9,1.3,2.6,1.4];
m=[1,2;26,90;1,2;120,0;1,0];
ba = intersect(b,a)
ba = 1×3
1.2000 1.3000 1.4000
ma = intersect(m,a)
ma = 0×1 empty double column vector
... but there's no intersection between m and a. I don't know what rules you use to get the results you describe. Is ma a function of the indices of the intersection of b and a?
[ba idx] = intersect(b,a);
ma = m(idx,:)
ma = 3×2
1 2 1 2 1 0
Benjamin Kraus
Benjamin Kraus am 2 Mär. 2022
I'm having some trouble understanding your question.
If I understand correctly, you are trying to calculate the values in ba and ma based somehow on the values of a, b, and m, but I don't see how you calculate the values in ma from any of the vectors, and ba appears to be equal to a. Can you give more description (in words) of the algorithm you are hoping to use to derive ba and ma based on a, b, and m?

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Rik
Rik am 2 Mär. 2022
Bearbeitet: Rik am 2 Mär. 2022
There are two issues with the solution by Arif:
  • It uses find on a logical array, but that is only used to index an array
  • It assume the the first column in m will be 1 for all the rows we're looking for
The edits below solve both.
a=[ 1.2,1.3,1.4];
b=[ 1.2,0.9,1.3,2.6,1.4];
m=[1,2;26,90;1,2;120,0;1,0];
[L,idx]=ismember(a,b); % find the element of a in b
ba=a(L)
ba = 1×3
1.2000 1.3000 1.4000
ma=m(idx(L),:)
ma = 3×2
1 2 1 2 1 0

Arif Hoq
Arif Hoq am 2 Mär. 2022
Bearbeitet: Arif Hoq am 2 Mär. 2022
try this:
a=[ 1.2,1.3,1.4]; % by array "a" i need to sort the values in array "b" and "m"
b=[ 1.2,0.9,1.3,2.6,1.4];
m=[1,2;26,90;1,2;120,0;1,0];
[idx]=find(ismember(b,a)); % find the element of a in b
ba=b(idx)
ba = 1×3
1.2000 1.3000 1.4000
ma=m((m==1),:) % find the first element=1
ma = 3×2
1 2 1 2 1 0

Kategorien

Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by