Replace matrix elements with zero apart from specific vectors

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 Comments

What you want is unclear to me.
Can we create a smaller toy example, to understand the concept better? Suppose I just have
N1 = [2 3 5];
N2 = [7 11 13];
N3 = [17 19 23 29]; % Using 1x4, instead of 1x41, for simplicity
Give me an example of what the bigger input matrix looks like (maybe just using 2x12), and what the desired output would be.
Okay, sorry for that.
So from a model I built through PDE, I have this big matrix of 2x28000. From this matrix, I extracted 8 vectors that represent specific nodes I need for my analysis. The point is when I solve the PDE, the solver input requires a matrix being the same size of the number of nodes (the big matrix itself), therefore I thought that removing all the other nodes values from the big matrix and keeping the N-vectors values could do the job.
Or, following your input, I can say the big matrix is:
x=[0 2 3 5; 7 11 13 6; 4 5 6 7; 17 19 23 29];
what I want is:
x=[0 2 3 5; 7 11 13 0; 0 0 0 0; 17 19 23 29];
Hope it is clearer now.
Thank you for your help.
So, for the example you posted, you want to replace [4 5 6 7] in the array x with 0?
Yes, but also [7 11 13 6] with [7 11 13 0].
Also, my bad for providing not the best example, but for the first row [0 2 3 5], it's just a case that the first number is 0. Any number there should also be replaced with a 0.
Thank you.
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...
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.

Sign in to comment.

 Accepted Answer

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 Comments

Hi,
Thank you for answering. I see your point and appreciate your help. Although I don't know the reason why x_new for me results in a zero matrix of the appropriate size. As if it is not substituting the vectors inside the zero matrix. I see that your code is working so I don't get why mine isn't.
Can you try saving the matrix and vectors into a mat file and uploading it here? Then someone can take a look.
Hi Voss,
The script you sent doesn't seem to work if N1 to N8 are 2xn arrays instead of 1xn vectors. Any idea how to implement this as well? Thank you in advance.
Does the script work for 1xn vectors?
I can modify it to work for 2xn arrays, but before I spend time doing that, I want to make sure it works on your actual data using the actual vectors referred to in the question, which are all 1xn.
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
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.

Sign in to comment.

More Answers (0)

Asked:

on 11 Jul 2023

Commented:

on 18 Jul 2023

Community Treasure Hunt

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

Start Hunting!