how to use sum and isnan across two different matrices

3 vues (au cours des 30 derniers jours)
Fede C 2018 London
Fede C 2018 London le 20 Juil 2021
Hi all,
How does one use sum and isnan to compare values in rows located in different matrices?
sum(isnan(data(i,2:end)),matfor2(:,1))>=1
doesn't work- data and matfor are the two matrices. I'm simply trying to add, for each iteration, the row in the columns of matrix data (from the 2nd onwards), with the 1st (non-zero) column of matfor2. The point of that line is to truncate the two columns at the point where both have non NaN values.
if I did this:
%if sum(isnan(data(i,:)))>=1
I wouldn't get an error message. ALL the data would be truncated to where the series with the most amount of NaNs starts getting values. But I don't want that. I want the loop to extract the largest possible span of data for combinations of two series.
I'm doing this in a loop and what I will be doing in the loop (won't present it here for clarity) requires stuff done to combinations of two columns, one -the 1st column in data- that doesn't change, and the other that changes at each iteration. The issue is that each time series may have different starts and ends in terms of data availability. So the span of the data, for each combination of column 1 and column ii, varies. For that reason, I have to adjust the length of the data every time. The way I've approached this is as follows:
[n,l] = size(data); %find length of data (raw, including NaN rows)
B = cell(1,nCols-1); %preallocate a cell to store matrices of different row length
matfor2=zeros(n,2);
matfor2(:,1) = data(:,1);
for ii= 2:nCols
%loop to pair match-wise rows without adjacent NaNs (two columns at a
%time)
for i=1:n;
if sum(isnan(data(i,2:end)),matfor2(:,1))>=1
i=i+1;
else
break
end
end
B{ii}=matfor2(i:n,ii)
end
  10 commentaires
Jan
Jan le 21 Juil 2021
@Fede C 2018 London: The problem is getting less clear from comment to comment. Do not try to be as explicit as possible, but you need a much more abstract view on the data to convert the idea to code. I have no idea what "RGDP" is an Matlab does not know it also. See the data as numerical matrices and the meaning does not matter. Now explain, what you want to do with these numbers.
Compare:
  1. I have an income of the person John Doe, living in Liverpool, born 1998, and a pile of money stolen from an elderly women, while she took in nap in the garden. When I put both amounts of money in a bag, how much do I have?
  2. Variables: x, y. Get: x + y.
Let this inspire you :-) Break down your idea until they can be solved with variables and operators.
Fede C 2018 London
Fede C 2018 London le 21 Juil 2021
thank you all for your comments. I finally worked it out.

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by