MATLAB Answers

Joel Bly
0

make true statements return value that it is associated with

Asked by Joel Bly
on 12 Apr 2019
Latest activity Commented on by Guillaume
on 16 Apr 2019
So I'm basically trying to get the values of the intersects of 2 matrices in the same position on the matrix they were previously. I'm trying to do this with a huge 350 row data set that has 4 columns. I already have a for loop to set up for the intersect values as well as using ismember function to know whether there is a duplicate that exist in a certain position. here's what comes out of both values.
O(1,:) = 12.7 0 0 0
L(1,:) = 0 1 0 0
Now all that's left is moving the 12.7 value to where the 1 value is without changing the 0's in the L matrix. and I want to do it with the entire data set. I know I would need another loop, but I don't even know how to go about doing this. and there hasn't been a question on here comparable to my problem. PLEASE HELP.
I HAVE BEEN AT IT FOR LIKE 2 WEEKS!!!!! I'm DYING INSIDE

  3 Comments

WHAT"S YOUR DESIRED OUTPUT??
And I suppose row two would be different? (i.e. 1 is replaced with some other number) Or are all ones replaced with 12.7?
my desired output is literally
somevariable = 0 12.7 0 0
everytime there's a number that is an intersection. ismember shows 1. Each number would be different.
so if the result is this:
somevariable = 1 0 1 0
two = 6 7 0 0
desiredvar = 6 0 7 0
that's what I want, but I don't know how to get there

Sign in to comment.

2 Answers

Answer by Walter Roberson
on 12 Apr 2019
Edited by Walter Roberson
on 12 Apr 2019

desiredvar = zeros(size(somevariable), class(two));
ind = find(somevariable == 1);
desiredvar(ind) = two(1:length(ind));
In the special case where somevariable is already logical, you could also write
desiredvar = zeros(size(somevariable), class(two));
desiredvar(somevariable) = two(1:nnz(somevariable));
You have not really defined what you want to have happen if somevariable and two are not the same length.

  3 Comments

the first code gave me something, but not what I want. the numbers it put in it's place weren't the right values. these number like 12.7 are row by row, so I was hoping to have them say in that row. unfortunately they didn't; numbers that didn't exist in the row prior was there in the place of the 1. but this is progress however. thank you so much. if you got anymore suggestings please shoot them my way. if the code can do these row by row that way the numbers don't get mixed up that would be awesome. that's why I need a loop, I believe.
so if I do it for just one row, it works, but not for all rows. How do i do it so that I make it work for all 348 rows in the 4 column matrix
nrow = size(O, 1);
ncol = size(O, 2);
output = zeros(nr, ncol);
srccol = 1 * ones(nr, 1);
for C = 1 : ncol
mask = find(L(:,C) == 1);
ind = sub2ind([nrow, ncol], mask, srccol(mask));
output(mask, C) = O(ind);
srccol(mask) = srccol(mask) + 1;
end
I think it would be possible to do without a loop, but I don't think it is worth doing.

Sign in to comment.


Answer by Guillaume
on 12 Apr 2019
Edited by Guillaume
on 12 Apr 2019

" I already have a for loop to set up for the intersect values "
It's very likely that a loop is not needed at all. Show us that code to know for sure.
"Now all that's left is moving the 12.7 value to where the 1 value is without changing the 0's [...] I know I would need another loop"
No, loop absolutely not needed:
O = [12.7 0 0 0];
L = [0 1 0 0]; %double or logical
L(logical(L)) = O(1:nnz(L)); %if L is of class double
L(L) = O(1:nnz(L)); %if L is logical

  4 Comments

Show 1 older comment
"this code did not work at all."
Copied straight from matlab command window:
>> O = [12.7 0 0 0];
L = [0 1 0 0]; %double or logical
L(logical(L)) = O(1:nnz(L))
L =
0 12.7 0 0
Exactly what was asked.
In any case, rather than saying it doesn't work explain why. If you get an error, give the full text of the error message. If it's not what you expected, explain...
weird tone in the writing but yeah it did not give me the result I wanted at all because I'm doing them for multiple rows of the matrix. yours only did the top row and messed up everything else. So it's not exactly the fact it didn't work, but rather that it didn't do what I needed it to do. Please refrain from using aggressive tone. Unless I'm reading that wrong. pretty please.
You wrote (unfortunately, not valid matlab, so we have to interpret):
so if the result is this:
somevariable = 1 0 1 0
two = 6 7 0 0
desiredvar = 6 0 7 0
that's what I want, but I don't know how to get there
>> somevariable = [1 0 1 0];
>> two = [6 7 0 0];
>> desiredvar = somevariable;
>> desiredvar(logical(desiredvar)) = two(1:nnz(desiredvar))
desiredvar =
6 0 7 0
How did that not do what you ask?
Now, if you need the same for each row of a matrix, then give an example, using valid matlab syntax so there's no ambiguity, of the matrices inputs (and corresponding desired output).
The above can always be wrapped in a loop operating on each row of the matrix obviously but most likely it can be done without a loop, e.g.:
tofill = [0 1 0 1;
1 1 0 0];
filler = [10 20 30 40]; %no idea if that's how your input is. Fill by ROW
tofill = tofill'; %matlab works on columns. So transpose
tofill(logical(tofill)) = filler(1:nnz(tofill)); %fill
tofill = tofill' %tranpose back

Sign in to comment.