Add a value in Matrix A from specific location in matrix B

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

How are we supposed to know what to choose? What x and y represent?
x and y does not change, it's an input. This is my first question posted, you´ll have to excuse my badly formulated problem.
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.

Sign in to comment.

 Accepted Answer

Well, if you really don't know the spot at which to assign you're stuck. But, if that's just poorly chosen words and you mean you don't know the syntax for the indirect assignment,
>> A=[1:10]';
>> N=[2 8 9];
>> Z=[44 21 pi];
>> A(N)=Z
A =
1.0000
44.0000
3.0000
4.0000
5.0000
6.0000
7.0000
21.0000
3.1416
10.0000
>>
QED, I think???

2 Comments

Thank you very much thats what i needed. I also have another question regarding loops ( i think?). Using your example above. If i would like to assign the elements in Z by user inputs, and the number of inputs is the same as N, how do i code that?
edit: Solved myself, thanks anyway!

Sign in to comment.

More Answers (1)

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

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...
Give an example...I can't parse the above.

Sign in to comment.

Categories

Tags

Asked:

on 19 Nov 2013

Commented:

dpb
on 19 Nov 2013

Community Treasure Hunt

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

Start Hunting!