How to create vector after import data from .xls using for loops?

12 views (last 30 days)
First I using command xlsread to import some data from excel and got vector row (or column it's never mind), then I try to create new matrix using for loops and if command, but I have problem, because I don't know how to do that... :)
Example: I import this
x=[1 5 3 7 10 10 10 15 4 8 3 5 11 19 10 10 10]
I want to create something like this:
a=[1 5 3 7] and b=[15 4 8 3 5 11 19]
,and new x=[mean(a) mean(b)]
It's necessary to use loops because every excel file that I import have different number of cell, for example: data from new excel is
x1=[1 2 3 10 10 10 4 5 6 7 8 9 11 12 13 10 10 10]
That the reason why I need block of code. Number 10 that is repeated is area that separate group of number, and I wanna to use that condition to create new vectors.
Thanks for your help.
  3 Comments
Bojan
Bojan on 19 Dec 2012
Yes it can. My need function to split x into n arrays. Where number 10 is zone between segments.
Bojan
Bojan on 19 Dec 2012
Yes, I use number 10 for example. This is the imported vector from .xls file:
x1 =
2.7523
5.5061
5.5171
5.5226
2.1736
100.0000
100.0000
100.0000
2.7587
4.1258
4.1102
4.0986
4.0916
1.4441
100.0000
100.0000
100.0000
0
2.0269
3.1598
3.1164
3.0886
3.0762
3.0708
3.0666
0.1945
100.0000
100.0000
100.0000
2.0194
2.0772
2.0506
2.0308
2.0135
2.0026
1.9958
1.9863
1.9782
1.0657
100.0000
100.0000
100.0000
0.8040
1.5448
1.4748
1.5440
1.6129
1.6061
1.5924
1.5766
1.5653
1.5313
1.5560
1.5495
1.4530
,my need vector:
a = [ 2.7523
5.5061
5.5171
5.5226
2.1736]
b=[2.7587
4.1258
4.1102
4.0986
4.0916
1.4441]
....
and finally:
x1=[mean(a) mean(b)]

Sign in to comment.

Accepted Answer

Bojan
Bojan on 24 Dec 2012
Thank everyone for trying to help me, but I find solution by my self. Maybe is little complicated, but it works, and that is most important... :)
I use "xlsread" command to import data from excel and I get matrix x1, then I use for loop to get my new matrix x2.
Code:
x2=zeros(13,7);
% caunters
m=1; n=1; t=1;
for k=m:length(x1)
if x1(k,:)~=100
x2(t,n)=x1(k,1);
t=t+1;
else if (x1(k,:)==100 && (x1(k+1,:)==100 && x1(k+2,:)==100))
m=k+3;
n=n+1;
t=1;
continue
end
end
end

More Answers (1)

Image Analyst
Image Analyst on 19 Dec 2012
This is trivial if you have the Image Processing Toolbox - it's one line:
segments = regionprops(x1~=10, x1, 'PixelList', 'MeanIntensity'); % Get all your a's, b's, etc.
The above line will get you all the segments (all their values into individual arrays), and the mean values of all the arrays.
Question - is it always 10 that is the "dividing" zone between segments? Or is it any number that repeats?
  4 Comments
Bojan
Bojan on 21 Dec 2012
I don't understand your question, how you mean "all over the place"? Forget number 10, I was using that for example. The real vector is with dividend 100, and 0 is part of the data. Now I wont to use vector x1 (with dividend 100) to create vector that content frst group of data (vector a). Then second vector (vector b) with data between dividend 100, and so on until the end. In a previous comment I placed a vector x1 that I imported from Excel, and now me need the vectors a and b ... and so on until the end. Thanks for helping me.
Image Analyst
Image Analyst on 21 Dec 2012
By all over the place I meant that the 10 or 100 or whatever can appear in stretches of any length in any position. It's not like you can just assume that each stretch of 100 occurs every 6th element throughout the array. If it did occur with such predictable periodicity you can just use normal, good old indexing to extract out the subarrays. However since your 100's occur at elements 6, 15, 27, etc. in random locations, that's what requires you to use regionprops which can handle that kind of data. The code is the same as I first gave you:
separationNumber = 100; % or 10 or whatever you want.
segments = regionprops(x1 ~= separationNumber , x1, 'PixelList', 'MeanIntensity');

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!