Convertig Matlab code to R code

Hello again,

I need help to convert a Matlab script to R script. I have already converted several things, but some functions or codes I can't understand them and I have searched without finding equivalences in R.
The whole script is based on a netCDF file with sea surface temperature data (sst). I managed to open it, load latitude, longitude, time, sst.
But at the moment of loading a mask to delimit the continents and to make maps only with the oceans in color, I get stuck in the conversion.

Could somebody explain me what these commands mean and how I could translate them?
I will put the Matlab code and the one I made for R. Please let me know if there is something wrong.

Mat   lon=ncread('X164.73.83.146.280.11.37.37.nc','lon');    #no problem with this
R        lon <- ncvar_get(datos_nc, "lon")

Mat  lat=ncread('X164.73.83.146.280.11.37.37.nc','lat');    #no problem with this
R       lat <- ncvar_get(datos_nc, "lat", verbose = F)

Mat  sst=ncread('X164.73.83.146.280.11.37.37.nc','sst');    #no problem with this
R       sst <- ncvar_get(datos_nc, "sst")

Mat  sst=ncread('X164.73.83.146.280.11.37.37.nc','sst');    #no problem with this
R       sst <- ncvar_get(datos_nc, "sst")

Mat  mask=ncread('lsmask.nc','mask');    #no problem with this
R       masc <- ncvar_get(mask_nc, "mask")

Mat  ind=find(mask==0);   #here I've started with problems. 
R       masc <- image(lon,lat2,masc==0)

After this I don't really know what to do. Spending today searching but have no clue what these steps mean:

mask(ind)=NaN;
sst=permute(sst,[3 1 2]);
mask_r=repmat(mask,1,1,360);mask_r=permute(mask_r,[3 1 2]);
sst=sst.*mask_r;

Thank you very much!

Seems like the objective is to multiply two three dimensional arrays together. There are a few steps beforehand to re-arrange the dimension of mask_r so that they're consistent with sst such that they can be multiplied.

The multiplication is an element-wise product (.* in Matlab, * in R), not an inner product (* in matlab, %*% in R).

Thank you Arthur. Yes, I understand the idea is to multiply two three dimensional arrays together, but I'm not getting it. I've tried several different ways.
I don't understand what this means either:

mask=ncread('lsmask.nc','mask');
ind=find(mask==0)
mask(ind)=NaN

I think he wants to indicate that all the continents (mask) are left blank so that only the ocean remains in color. But I don't know how to do it in R.

I would also like to know if the function conversions (permute and repmat) I made are correct or not:

M sst=permute(sst,[3 1 2])
R sst <- aperm(sst, c(3,1,2))

M mask_r=repmat(mask,1,1,360)
R mask_r <- array(mask, c(360, 180, 360))

Thank you very much!

ind=find(mask==0)
mask(ind)=NaN

I think Matlab find is similar to R which.

These two lines of code should replace 0 values with NaN.

Perhaps in R,

ind <- which(mask == 0)
mask[ind] <- NA

On the subject of the permute vs. repmat. I think that will take come careful work. Will be tricky without access to Matlab.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.