Add a value in Matrix A from specific location in matrix B
Show older comments
Hello.
Lets say i have a A = 3000x1 matrix, and a matrix N = [49; 106; 198] where 49,106 and 198 is the locations of a variables in A. Then assign a value to that specific variable.
So i could assign the first value like A(49,1) = x and the second A(106,1) = y .
My problem is that i don't know the number of variables in N, and i don't know which spot in A i should assign the respective values to.
Thank you!
3 Comments
Azzi Abdelmalek
on 19 Nov 2013
How are we supposed to know what to choose? What x and y represent?
Erik B
on 19 Nov 2013
dpb
on 19 Nov 2013
It's no better, yet, unfortunately. There's got to be a clear statement of the inputs and the desired output. "I don't know" doesn't count, nor do things like a mismatch in numbers of inputs and outputs unless the rule for what to do with them is given.
Show a specific example in its entirety; perhaps others can generalize from that.
Accepted Answer
More Answers (1)
Roger Stafford
on 19 Nov 2013
Edited: Roger Stafford
on 19 Nov 2013
If x is a given scalar value, you can find the index or indices (if any) in A that reference it with:
p = find(A == x);
There could be one, many, or no values in p depending how many matches to x there are in A. This works if you require an exact match. If there could be some small mismatch that you allow, you could use this:
p = find(abs(A-x)<=tol);
where 'tol' is the maximum match error you will tolerate.
2 Comments
MacKenzie
on 19 Nov 2013
Hi Roger, jumping in here: what if I have a matrix that is two different row lengths? I'm trying to find the index of a 2 column matrix in another (the original source 2 column matrix). Where I want the first index, but it must be greater than the last found index...
dpb
on 19 Nov 2013
Give an example...I can't parse the above.
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!