Replace matrix elements with zero apart from specific vectors

3 次查看(过去 30 天)
Hello,
I have 8 vectors, N1 to N8 which belong to a bigger matrix (2x28000). Four of the N-vectors are 1x3 and the other four are 1x41.
I want to replace the values of the big matrix with zeros apart from those belonging to the N-vectors.
I managed to extract the N-vectors from the bigger matrix using findNodes in PDE toolbox, e.g.:
'N1 = findNodes(model.Mesh,"region","Edge",1);'
But what I actually need is the whole bigger matrix with only the N-vectors values (and all the others =0) instead of just the N-vectors.
Hope it is clear.
Any idea on the method?
Thank you so much.
  6 个评论
dpb
dpb 2023-7-11
But by what logic is one to know which elements to replace is the Q? still indeterminate. You can replace any value by any other; the problem is having a way to generate the indices of the locations to change programmatically.
I scanned through the whole conversation and don't see anywhere that is defined; you start out with something that is 2xN and some other vectors of some size that don't seem somehow related to that dimension and when @the cyclist asked for a smaller example that illustrated the issue, then your reply came back with something that is 4x4 but no associated vectors; just a set of points seemingly arbitrarily set to zero.
It's no wonder everybody is confused trying to keep up...
Stefano Russo
Stefano Russo 2023-7-11
Okay, once again sorry for my bad explanation.
Let's say I have a an array 'x' 2x28000 made of random numbers.
Let's consider I extract from 'x' a number of vectors named N1,N2,...N7,N8. As the numbers in 'x' are random, the numbers inside each vector will be a specific arrangement of random numbers, each unique.
Now, more specifically, if we consider N1= [3 485 71], since N1 is extracted from the array 'x', there must be a portion of 'x' which has [3 485 71] located somewhere inside it. This statement is also true for all the other 7 vectors.
What I'm asking is: is there a way to match all the cells of the vectors with the array, and then change the value of all the resulting non-matched cells into 0?
Hope it is clearer now.
PS: regarding the example I made before:
In the first row of the array, the vector N1=[2 3 5] is included along with a 0 in the 1x1 cell, therefore nothing to do here for my specific case because the value is already 0.
[7 11 13 6] in the second row includes the N2 vector [7 11 13], so I want the remaining cell with value 6 to have value 0.
As in the third row there is no correspondance with any of the vectors provided, all the values in this row must be 0.
For the last row, N3 matches it fully, therefore no further adjustment is needed.
Thank you everyone for your support.

请先登录,再进行评论。

采纳的回答

Voss
Voss 2023-7-11
N1 = [2 3 5];
N2 = [7 11 13];
N3 = [17 19 23 29];
x = [0 2 3 5; 7 11 13 6; 4 5 6 7; 17 19 23 29]
x = 4×4
0 2 3 5 7 11 13 6 4 5 6 7 17 19 23 29
Rather than replacing unwanted elements of x with zero, you can think of it as starting with a matrix of all zeros the same size as x, and placing vectors into the matrix at the same locations they are in x.
% collect the vectors in a cell array:
C = {N1,N2,N3};
% get the length of each vector:
lenN = cellfun(@numel,C);
% get the total number of vectors:
nc = numel(C);
% make a new matrix, same size as x, initially all zeros:
[mm,nn] = size(x);
x_new = zeros(mm,nn);
% loop over rows of x:
for ii = 1:mm
% loop over vectors:
for jj = 1:nc
% find the locations where vector jj starts in row ii of x:
idx = strfind(x(ii,:),C{jj});
% place vector jj at those locations in row ii of x_new:
for kk = 1:numel(idx)
x_new(ii,idx(kk):idx(kk)+lenN(jj)-1) = C{jj};
end
end
end
% see the result:
x_new
x_new = 4×4
0 2 3 5 7 11 13 0 0 0 0 0 17 19 23 29
  6 个评论
Stefano Russo
Stefano Russo 2023-7-18
Hi @Voss,
Sorry for getting back this late. I thought I figured out how to do it by myself but I've just realised it is not working. You code for 1xn vector does work, would you mind modifing it accordingly for 2xn arrays?
I would truly appreciate it.
Thank you in advance.
Stefano
Voss
Voss 2023-7-18
I won't have time to look at that today, so I recommend posting a new question about it. More people are likely to see a new question than a comment here anyway.

请先登录,再进行评论。

更多回答(0 个)

标签

Community Treasure Hunt

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

Start Hunting!

Translated by