Info

此问题已关闭。 请重新打开它进行编辑或回答。

Relating two matrices when one contains real and non real numbers?

1 次查看(过去 30 天)
I have two matrices.
Bondsbystep is a 64X30000 matrix listing bond lengths at each step of a calculation.
a is a 1x30000 matrix listing the important bond number in Bondsbystep.
I'm trying to write a script so that the bond length (from Bondsbystep) corresponding to the right bond (listed in a) at each step is listed in a 1x30000 matrix. I have done this with a set of data which is all real integers using:
for i= 1:30000;
x=bondsbystep(a(1,i),:);
end
However using a set of data where some a(1,i) values are real, and some are NaN produces the error:
Subscript indices must either be real positive integers or logicals.
Ideally I would have a 1x30000 matrix where some values are real bond lengths, and some are 0 (or nan). This will enable me to plot bond change over time.
I've tried to use an if function of the following:
x2=nan(1,numel(a)); %creates 1x30000 matrix
for i=1:numel(a); %range of points
if a(1,i)==0; %condition to satisfy, if it's a real number do this
x2(i)=Bondsbystep(a(1,i),:);
end
end
But I think I missing something in the way I'm relating the two matrices. Any help or pointers on where I should be look is much appreciated.
Edit:
I have also tried using a if/else loop:
for i=1:30000; %range of points
if a(1,i) > 0; %condition to satisfy, if it's a valid number do this
x2=Bondsbystep(c(1,i),:);
else
if a(1,i)==NaN;
x2(1,i)=0;
end
end
end
Which runs but doesn't give me the correct output.
I've tried using the continue function as well, which again runs but not the correct output.
Perhaps this is a clearer example of what I need:
for i=1:30000;
if a(1,i) > 0;
x2(1,i)=Bondsbystep(c(1,i),:);
elseif a(1,i)==0;
x2(1,i)=0;
end
end
I have tried the above but got the error:
Assignment has more non-singleton rhs dimensions than non-singleton subscripts
Many thanks for any input.

回答(1 个)

Walter Roberson
Walter Roberson 2016-6-3
Your code has
if a(1,i)==0; %condition to satisfy, if it's a real number do this
x2(i)=Bondsbystep(a(1,i),:);
end
The first line succeeds only if a(1,i) is 0. You then use that 0 as an index into the Bondsbystep array, producing an error.
I suggest
if ~isnan(a(1,i)) && a(1,i) > 0; %condition to satisfy, if it's a valid number do this
x2(i)=Bondsbystep(a(1,i),:);
end
  2 个评论
Olivia Lynes
Olivia Lynes 2016-6-6
Thanks for the response, I tried swapping your line into my code and got;
In an assignment A(:) = B, the number of elements in A and B must be the same.
This is what I was writing:
x2=nan(1,numel(a)); %creates 1x30000 matrix
for i=1:numel(a); %range of points
if ~isnan(a(1,i)) && a(1,i) > 0; %condition to satisfy, if it's a valid number do this
x2(i)=Bondsbystep(a(1,i),:);
end
end
All the matrices are the same size (1x30000), so I'm not sure what it's referring to. Is there something I'm missing?
Many thanks
Guillaume
Guillaume 2016-6-6
x2(i) = Bondsbystep(a(1,i),:);
x2(i) is a single number. Bondsbystep(x, :) is a whole row of Bondsbystep, in this case 30,000 numbers. You can't put 30,000 numbers in a slot for just one number, hence the error you get.
Maybe you meant:
x2(i) = Boundsbystep(a(i), i);
I.e. takes the a(i)th row of the ith column and put it in x2(i).
Note that since a is a vector, it makes more sense to use linear indexing, a(i), as you've done for x2, rather than 2D indexing, a(1, i). The former will work regardless if a is a column or row vector, your 2d indexing will break if a is a column vector. So, I would use:
if ~isnan(a(i) && a(i) > 0 %it's more readable as well.

此问题已关闭。

产品

Community Treasure Hunt

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

Start Hunting!

Translated by