Index an array using specific indices

13 visualizzazioni (ultimi 30 giorni)
fadams18
fadams18 il 4 Nov 2022
Commentato: Book il 2 Ott 2025
I have a vector
x = [0,1,1,0,1,...,N] % x is vector containing either 1s or 0s of size N = 107199.
then I have another vector s, which contains index locations in x.
s= [23,100,500, 9888,....N] % s is of size 1x518
so I want to filter s to contain only indices where the values in x are 1s.
I tried something like this,
idx = x(s)~=1;
new_s = s(idx);
but it gives me this error Index exceeds the number of array elements (107520).. any idea what im doing wrongly?

Risposte (4)

Daniel Neubauer
Daniel Neubauer il 4 Nov 2022
x=[1 0 1 1 0 0 0 0 1 0];
s=[9 4 6];
snew=s(x(s)==1)
snew = 1×2
9 4

DGM
DGM il 4 Nov 2022
Try
% some test vectors
x = randi([0 1],1,20)
x = 1×20
0 0 0 1 1 0 1 0 1 0 1 1 1 0 1 0 1 1 1 0
s = randi([1 numel(x)],1,10)
s = 1×10
9 6 20 9 12 9 17 11 7 3
% this is the content selected by s
x(s)
ans = 1×10
1 0 0 1 1 1 1 1 1 0
% and then this would be cases where s selects 1s within x
new_s = s(x(s)==1)
new_s = 1×7
9 9 12 9 17 11 7
  3 Commenti
Book
Book il 2 Ott 2025

a = input('Rows for x1: '); b = input('Columns for x1: '); x1 = zeros(a,b); for i = 1:a for j = 1:b x1(i,j) = input('Enter elements for x1: '); end end

c = input('Rows for x2: '); d = input('Columns for x2: '); x2 = zeros(c,d); for i = 1:c for j = 1:d x2(i,j) = input('Enter element for x2: '); end end

disp('Matrix x1:'); disp(x1) disp('Matrix x2:'); disp(x2)

Book
Book il 2 Ott 2025

% Step 1: Create two 3x3 matrices x1 = [1 2 3; 4 5 6; 7 8 9]; % you can replace with your own values x2 = [9 8 7; 6 5 4; 3 2 1]; % you can replace with your own values

% Step 2: Perform the required operations

% a) y1 = x1 - 2x2 y1 = x1 - 2*x2;

% b) y2 = 3x2 + (1/2)x1 y2 = 3*x2 + 0.5*x1;

% c) y3 = (2/3)x1x2 (matrix multiplication) y3 = (2/3) * (x1 * x2);

% d) y4 = x2x1 (matrix multiplication) y4 = x2 * x1;

% e) y5 = x1^(-4) + x2^3 % x1^(-4) means inverse(x1)^4, must be invertible try y5 = (inv(x1)^4) + (x2^3); catch disp('Matrix x1 is not invertible, choose another set of values.'); y5 = NaN; end

% Display results disp('y1 ='); disp(y1); disp('y2 ='); disp(y2); disp('y3 ='); disp(y3); disp('y4 ='); disp(y4); disp('y5 ='); disp(y5);

Accedi per commentare.


dpb
dpb il 4 Nov 2022
Well, if size N = 107199 while s is of size 1x518, then 107199 > 518 so any location in x past numel(s) is going to be an out of bounds reference.
It's not clear in isolation what relationship there is (if any) between x and s so why one would expect those two to be commensurate is puzzling.
Perhaps you're looking for locations that are in s that were found in some other fashion and wanting to know if there's a corresponding matching "1" value in x at the specific location? That's about the only way I can think of at the moment to relate the two -- if that were the case then try
new_s=s(ismember(s,find(~x)));

fadams18
fadams18 il 4 Nov 2022
Ah i got it. In my original code I was doing something to s before the indexing, which was chanching its values. My code also works.
Thanks for your insights and time guys, @KALYAN ACHARJYA @dpb @Daniel Neubauer @DGM.

Categorie

Scopri di più su Matrices and Arrays in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by