Linear Interpolation in MATLAB [closed] - matlab

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have two arrays the first one represents the time axis with time stamp 1 min
time=[0,60,60,120,180,240,300,360,420,480,540]
and the second array represents a data values as follows
data=[18,12,12,0,7,9,6,8,12,18,0]
what im trying to do is two things:
1-I would like to fix the time axis to have 1 second time stamp
2-Perform linear Interpolation as follows:
for example i have
enter image description here
and i would like to have sth like this:
enter image description here
In case of time repetation like the repated 60 seconds the duplication should be deleted

You can remove duplicates (the first value is kept) with
time = [0,60,60,120,180,240,300,360,420,480,540];
data = [18,12,12,0,7,9,6,8,12,18,0];
[time_u unique_indeces] = unique(time);
data_u = data(unique_indeces);
clear unique_indeces;
and interpolate with
time_i = linspace(min(time), max(time), max(time) - min(time) + 1);
data_i = interp1(time_u, data_u, time_i);
I prefer linspace because I usually want to set the number of data points and not the space between points but you can also use min(time):max(time) or time(1):time(end) instead.
This code will also sort your data points by time.

Function interp1 does the job:
time=[0,60,120,180,240,300,360,420,480,540];
data=[18,15,0,7,9,6,8,12,18,0];
time_1s = 0:540;
data_interpd = interp1(time, data, time_1s);
Note: I have manually deleted the first duplicate value at time 60. If there is only one value to remove (always at same place), I think the best is to remove it by using a mask, since unique removes the second occurrence of duplicates and not first one.

Related

How to make a new matrix based on column values in an old matrix? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am very new to Matlab and coding in general, so I apologize if this is a basic question.
I have a matrix of three columns (data1) where the first column refers to time (s).
I would like to make a new matrix (bout1) consisting of entire rows of the matrix data1 based on the values in the first column (so, for example, in a range from 30 s to 120 s).
I know how to extract the rows based on the row number:
bout1 = data1(361126:391643,:)
but not based on the values in a specific column.
You can use the find function (see here) to find the rows you need, like this:
time = data1(:, 1);
i = find(30 <= time & time <= 120);
bout1 = data1(i, :);

Basics on Matlab function Unique [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm studying the function unique, which looks simple at a first sight but I don't understand some properties.
I created a matrix A and tried to analyze the outputs:
A=[5 2 4 5;
1 1 3 4;
6 1 2 3]
[C0,IA0,IC0]=unique(A)
[Cr,IAr,ICr]=unique(A,'rows')
[Cf,IAf,ICf]=unique(A,'first')
In C0 the logic of the output is "create a vector in which there are values that appears at least one time"
But I don't know the meaning of IA0 and IC0. I just know the relation that C=A(IA0) and A=C(IC0). Are these 2 output created only to satisfy this two relation? So why should I be intersted in their outputs?
In Cr ('rows' example) the logic of the output is "give me back the rows of the original matrix A but sorted ascending. Also, if you find at least two or more rows that repeat with same values and order, show that row only once in Cr output"
The logic of IAr is very intuitive: "give me back the index that must follow the Cr output to order the rows." So in my example gives me back a vector like IAr=[2;1;3]. Thus the second row of the original matrix A must be the first in the Cr output, the first row in A matrix must be the second in Cr...
But I still don't understand the output ICr
In Cf ('first' example) gives me back the same output as C0. And it's not really clear to me how to use properly this function.
Can anyone gives me a simple explanation about how this function works?
Are there any simple practical examples in which I can take advantage of these other outputs?
Matlab is a generic tool. So asking "why" some functions give extra output just because it might be useful to someone for some reason is difficult to answer.
IA0 gives the indices of the last elements that might have been chosen by unique. IC0 contains indices to replicate A using only the unique elements. Why do they exist? You may not only be interested in unique values, but also where to find them in A.
You are misinterpreting how rows works. It simply treats the rows of your matrix as atomic and returns the unique rows. To get a better idea of how it works, run A(3,:) = A(2,:) before calling unique. Then the idea behind IA0 and IC0 is the same as in the previous case, except now with rows.
The first option only changes how Matlab chooses the indices of IA0. Again, the reason behind this is that Matlab is a generic tool. You might be interested in finding the latest repeated occurence of each value or in the first. It depends on what you want to do.

Adding numbers whose sum is greater than 10 in brainfuck [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Is there an efficient algorithm that determine the sum of two numbers, even if these numbers are greater than ten?
the user can only enter the numbers one digit at a time. Assuming only positive numbers can be entered the algorithm to parse it is as follows.
initialize accumulator to 0
for each digit the user enters
multiply the accumulator by 10
add the new digit to the accumulator
You will need to handle when the user enters the enter key to finish entering the number, and the above algorithm only works for numbers up to 255 (assuming 8 bit cells).
From then on you have a cell with a number. Do this again to get a cell with another number, then you can simply add them together normally.
Using http://fatiherikli.github.io/brainfuck-visualizer/ you can see the value of each cell numerically, not having to use all that dumb ASCII conversion stuff. Then, just program in your inputs and the algorithm.
++++++>+++++ 6 plus 5
[<+>-]
Adds one to 6 and subtracts one from 5 each iteration, making 11.
The visualizer will show that the cell is 11.
If you want to preserve the y value, add a "temp" cell
++++++>+++++>[-] #0: 6 #1: 5 #2: 0
<[<+>>+<-]
>[<+>-]

Using repmat() in MATLAB [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I need to generate the trial order of my stimuli for an experiment consisting of 10 experimental blocks:
There should be 100 trials per block.
There are 20 images as stimuli.
Within each block, the 20 stimuli should
be shown 5 times each.
The order of the stimuli should be fully randomized.
(Meaning, NOT 1:20 in random order, then 1:20 in random order, and so on.
All 100 trials should be randomized across each block!)
I have to make a matrix that represents the trial order of my experiment, in which the rows represent the 10 blocks, and the columns represent the stimuli to
be shown in order from column 1 - to column 100.
I figured out that I should use the function repmat(), but I can't solve this.
This will do it, just adjust your values for the number of blocks and block size according to your needs. No repmat used though.
Nblocks = 10;
Nchoices = 20;
Ndisp = 5;
Ntrials = Ndisp*Nchoices;
array = ceil([Nchoices/Ntrials:Nchoices/Ntrials:Nchoices]);
perms = array(cell2mat(cellfun('randperm',mat2cell(Ntrials*ones(Nblocks,1),ones(Nblocks,1),1),'UniformOutput',0)));
It's a good idea to split the longer wrapped command into individual steps if you want to make sense of it in more depth. Look in particular at the documentation for individual functions and particularly ceil and randperm.

How can i increase speed of for loop in matlab? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to read the value of pixcels in image result to compare this value with some
I use to for loop
function GrdImg= GrdLbp(VarImg,mapping,LbpImg)
tic
p=mapping.samples;
[Ysize,Xsize]=size(result);
GImg=zeros(Ysize,Xsize);
temp=[];
cnt=1;
for n=0:p-1
temp(cnt)=2^n;
temp(cnt+1)=(2^p)-1-(2^n);
cnt=cnt+2;
end
for i=1:Ysize
i
for j=1:Xsize
if isempty(find(result(i,j)==temp(:,:)))==1
GImg(i,j)=sqrtm(Vresult(i,j));
end
end
end
but it works too slow, Could you help me what can I use instead of for loop?
Thanks a lot
You didn't really give enough information to answer your question - since, as was stated in the comments, you aren't doing anything with the values in the loop right now. So let me give you a few ideas:
1) To compare all the pixels with a fixed value, and return the index of all pixels greater than 90% of the maximum:
threshold = 0.9 * max(myImage(:));
prettyBigPixels = find(myImage > threshold);
2) To set all pixels < 5% of max to zero:
threshold = 0.05 * max(myImage(:));
myImage(myImage < threshold) = 0;
In the first case, the find command returns all the indices (note - you can access a 2D matrix of MxN with a single index that goes from 1 to M*N). You can use ind2sub to convert to the individual i, j coefficients if you want to.
In the second case, putting (myImage < threshold) as the index of the matrix is called logical indexing - it is very fast, and will access only those elements that meet the criterion.
If you let us know what you're actually doing with the values found we can speed things up more; because right now, the net result of your code is that when the loop is finished, your value Temp is equal to the last element - and since you did nothing in the loop we can rewrite the whole thing as
Temp = pixel(end);
EDIT Now that you show what you are doing in your inner loop, we can optimize more. Behzad already showed how to speed up the computation of the vector temp - nothing to add there, it's the right way to do it. As for the two nested loops, which are likely the place where most time is spent, you can find all the pixels you are interested in with a single line:
pixelsOfInterest = find(~ismember(result(:), temp(:)));
This will find the index of pixels in result that do not occur in temp. You can then do
GImg(pixelsOfInterest) = sqrt(result(pixelsOfInterest));
These two lines together should replace the functionality of everything in your code from for i=1:Ysize to the last end. Note - your variables seem to be uninitialized, and change names - sometimes it's result, sometimes it's Vresult. I am not trying to debug that; just giving you a fast implementation of your inner loop.
As of question completely edited I answer new rather than edit my former answer, by the way.
You can improve your code in some ways:
1. instead of :
for n=0:p-1
temp(cnt)=2^n;
temp(cnt+1)=(2^p)-1-(2^n);
cnt=cnt+2;
end
use this one:
temp=zeros(1,2*p);
n=0:p-1;
temp(1:2:2*p)=2.^n; %//for odd elements
temp(2:2:2*p)=2^p-1-2.^n; %//for even elements (i supposed p>1)
2.when code is ready for calculating and not for debugging or other times, NEVER make some variables to print on screen because it makes too long time (in cpu cycles) to run. In your code there are some variables like i that prints on screen. remove them or end up them by ;.
3.You can use temp(:) in last rows because temp is one-dimensional
4.Different functions are for different types of variables. in this code you can use sqrt() instead of sqrtm(). it may be slightly faster.
5. The big problem in this code is in your last comparison, if non elemnt of temp matrix is not equal with result specific element then do something! its hard to make this part improved unless knowing the real aim of the code! you may be solve the problem in other algorithm that has completely different code. But if there is no way, so use it in this way (nested loops) Good Luck!
It seems your image is grayscle or monocolor , because Temp=pixel(i,j) gives a number not 3-numbers by the way.
Your question has not more explanation so I think in three type of numbers that you are comparison with.
compare with a constant number
compare with a series of numbers
compare with a two dimensional matrix of numbers
If first or third one is your need, solution is very easy (absolutely in third one, size of matrix must be equal to pixel size)
Comparison with a number (c is number or two-dimensional array)
comp=pixel - c;
But if second one is your need, you can first reshape pixel to one-dimensional matrix then compare it with the series of number s (absolutely length of this serie must be equal to product of pixel rows number and columns number; you can re-reshape pixel matrix after comparison to primary two dimensional matrix.
Comparison with a number serie s
pixel_temp = reshape(pixel,1,[]);
comp = pixel_temp - s;
pixel_compared = reshape(pixel_temp,size(pixel,1),size(pixel,2)); % to re-reshape to primary size