Hey,
I'm trying to modify multiple matrix's elements at a time. considering the matrix a = zeros(3), I want to set the two values a(1, 2) and a(3, 3) to 1.
Instead of doing a(1,2) = 1, and then a(3,3) = 1, is there a way to do that on one line ?
I tried different variants of a([1 3], [2 3]) but it never gives me what I expect. (it modifies all combinations of both vectors, (1,2) (1,3) (3,2) (3,3)). I must be close, any hint anyone ? Thanks.
Greetings,
Patrick
|
|
0
|
|
|
|
Reply
|
Patrick
|
10/15/2010 3:32:03 PM |
|
"Patrick A." <m.maverick@gmail.com> wrote in message <i99s5j$ipo$1@fred.mathworks.com>...
> Hey,
>
> I'm trying to modify multiple matrix's elements at a time. considering the matrix a = zeros(3), I want to set the two values a(1, 2) and a(3, 3) to 1.
>
> Instead of doing a(1,2) = 1, and then a(3,3) = 1, is there a way to do that on one line ?
>
> I tried different variants of a([1 3], [2 3]) but it never gives me what I expect. (it modifies all combinations of both vectors, (1,2) (1,3) (3,2) (3,3)). I must be close, any hint anyone ? Thanks.
>
> Greetings,
> Patrick
A hint:
sub2ind
Oleg
|
|
0
|
|
|
|
Reply
|
Oleg
|
10/15/2010 3:39:04 PM
|
|
That's great thanks.
Another question, I have a 3D matrix, with only 2 dimension with arrays (first dimension fixed)
So for now I use something like :
a=rand(2, 3, 2);
d1=2;
d2=[1 2];
d3=[1 2];
a(sub2ind(size(a), linspace(d1, d1, length(d2)), d2, d3)) = 1;
this works but I don't feel comfortable with this linspace thing, is there a better way to generate a vector of x of length y ?
Greetings,
Patrick
|
|
0
|
|
|
|
Reply
|
Patrick
|
10/15/2010 4:08:04 PM
|
|
> a=rand(2, 3, 2);
> d1=2;
> d2=[1 2];
> d3=[1 2];
> a(sub2ind(size(a), linspace(d1, d1, length(d2)), d2, d3)) = 1;
> this works but I don't feel comfortable with this linspace thing, is there a better way to generate a vector of x of length y ?
help repmat
help meshgrid
|
|
0
|
|
|
|
Reply
|
Sean
|
10/15/2010 4:13:03 PM
|
|
"Patrick A." <m.maverick@gmail.com> wrote in message
news:i99u94$8un$1@fred.mathworks.com...
> That's great thanks.
>
> Another question, I have a 3D matrix, with only 2 dimension with arrays
> (first dimension fixed)
>
> So for now I use something like :
>
> a=rand(2, 3, 2);
> d1=2;
> d2=[1 2];
> d3=[1 2];
> a(sub2ind(size(a), linspace(d1, d1, length(d2)), d2, d3)) = 1;
>
> this works but I don't feel comfortable with this linspace thing, is there
> a better way to generate a vector of x of length y ?
>
> Greetings,
> Patrick
>
use indexing
num = 3
twelvenums = num(ones(1,12))
--
Loren
http://blogs.mathworks.com/loren/
http://matlabwiki.mathworks.com/MATLAB_FAQ
|
|
0
|
|
|
|
Reply
|
loren.shure (822)
|
10/18/2010 12:55:11 PM
|
|
|
4 Replies
387 Views
(page loaded in 0.056 seconds)
Similiar Articles: Changing multiple matrix element at one time - comp.soft-sys ...Hey, I'm trying to modify multiple matrix's elements at a time. considering the matrix a = zeros(3), I want to set the two values a(1, 2) and a(3, ... Synchronizing time series? - comp.soft-sys.matlabChanging multiple matrix element at one time - comp.soft-sys ... Synchronizing time series? - comp.soft-sys.matlab Changing multiple matrix element at one time - comp.soft ... charateristic matrix in thin film optics. - comp.soft-sys.matlab ...Changing multiple matrix element at one time - comp.soft-sys ... charateristic matrix in thin film optics. - comp.soft-sys.matlab ... Changing multiple matrix element at ... Multiple leaders with dimension - comp.cad.solidworksChanging multiple matrix element at one time - comp.soft-sys ... Multiple leaders with dimension - comp.cad.solidworks Changing multiple matrix element at one time - comp ... element-by-element multiplication - comp.soft-sys.matlab ...Changing multiple matrix element at one time - comp.soft-sys ... element-by-element multiplication - comp.soft-sys.matlab ... Changing multiple matrix element at one time ... chaning a part of cell arrays - comp.soft-sys.matlabChanging multiple matrix element at one time - comp.soft-sys ... Converting a cell array into a dataset - comp ... checking/setting part of an array. - comp.soft-sys ... Matrix element to Matrix element-range mutiplication - comp.soft ...Changing multiple matrix element at one time - comp.soft-sys ... Hey, I'm trying to modify multiple matrix's elements at a time. considering the ... how to delay every elements of a vector in simulink? - comp.soft ...Changing multiple matrix element at one time - comp.soft-sys ... how to delay every elements of a vector in simulink? - comp.soft ... Changing multiple matrix element at ... different length vectors -> one matrix - comp.soft-sys.matlab ...Changing multiple matrix element at one time - comp.soft-sys ... different length vectors -> one matrix - comp.soft-sys.matlab ... Changing multiple matrix element at one ... simple question about MODEL matrix - comp.graphics.api.opengl ...Changing multiple matrix element at one time - comp.soft-sys ... two simple time series questions - comp.soft-sys.matlab... not to economics but to ... way to measure the ... Changing multiple matrix element at one time - comp.soft-sys ...Hey, I'm trying to modify multiple matrix's elements at a time. considering the matrix a = zeros(3), I want to set the two values a(1, 2) and a(3, ... Changing multiple matrix element at one time - Newsreader - MATLAB ...Hey, I'm trying to modify multiple matrix's elements at a time. considering the matrix a = zeros(3), I want to set the two values a(1, 2) and a(3, 3) to 1. 7/26/2012 2:10:11 PM
|