필터 지우기
필터 지우기

for loop store values?

조회 수: 2 (최근 30일)
Phil Whitfield
Phil Whitfield 2018년 8월 1일
답변: Ashraf Rayed 2020년 5월 12일
so I am convinced there is a better way for me to approach the following code:
ftse = xlsread('ftse123.xlsx');
L = length(ftse);
s = floor(L/10);
f25 = ftse(:,3);
k=1;
h=1;
for i = 1:10
j=s*i;
h=s*(i-1);
if h== 0
h=1;
else
h=s*(i-1);
end
y{i} = f25(h:j,:);
end
x1=cell2mat(y(1));
x2=cell2mat(y(2));
x3=cell2mat(y(3));
x4=cell2mat(y(4));
x5=cell2mat(y(5));
x6=cell2mat(y(6));
x7=cell2mat(y(7));
x8=cell2mat(y(8));
x9=cell2mat(y(9));
x10=cell2mat(y(10));
I used copy and paste after struggling to save the values x1 etc, within the for loop.
Also noticed that x1 is has a length of 522 and not 523 like the rest?
The code achieves what I want/need but must be long and drawn out.
any advice would be great thanks.
  댓글 수: 2
Stephen23
Stephen23 2018년 8월 1일
편집: Stephen23 2018년 8월 1일
"I used copy and paste after struggling to save the values x1 etc, within the for loop."
Copy-and-pasting code is a sign that you are doing something wrong. Using numbered variables is a sign that you are doing something wrong. The solution to both of these: use indexing.
Please upload ftse123.xlsx so that we have something to work with.
Phil Whitfield
Phil Whitfield 2018년 8월 1일
that's when I posted after I started copy and pasting because I knew it was wrong.

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2018년 8월 1일
편집: Stephen23 2018년 8월 1일
I would recommend that you use readtable or importdata and use a table to store, group, and process your data. In lieu of that, here is some simple code to group the third column into ten groups:
>> N = size(ftse,1);
>> V = linspace(1,11,N+1);
>> V = fix(V(1:end-1));
>> C = accumarray(V(:),ftse(:,3),[],@(v){v});
It is much simpler and more efficient to access the cells of C using indexing. Do NOT create numbered variables.
"Also noticed that x1 is has a length of 522 and not 523 like the rest?"
Because ftse has 5225 rows. There is no way to split this into ten equally sized groups:
>> size(ftse)
ans =
5225 4
>> size(ftse,1)/10
ans =
522.5
Half the groups will have 252, half have 253 elements. My code spreads these evenly through C:
>> cellfun('size',C,1)
ans =
523
522
523
522
523
522
523
522
523
522
  댓글 수: 2
Phil Whitfield
Phil Whitfield 2018년 8월 1일
편집: Phil Whitfield 2018년 8월 1일
Thanks, is there a way to create a 523x7 and 523x3 matrix of C from the index?
normally I would do :
xx=[x1,x2,x3,x4,x5,x6,x7];
xxx=[x8,x9,x10];
also I appreciate the 522 thing, should've seen that thanks.
Stephen23
Stephen23 2018년 8월 1일
편집: Stephen23 2018년 8월 1일
"Thanks, is there a way to create a 523x7 and 523x3 matrix of C from the index?"
Not easily, because that does not match how many of each size data group that you actually have: five of each. So you could easily get 522x5 and 523x5 matrices:
M523 = [C{1:2:end}]
M522 = [C{2:2:end}]

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Ashraf Rayed
Ashraf Rayed 2020년 5월 12일
i have this code, i have to check the leaf area of some pictures in a folder and then i have save the area of those leaves by sequence in an excel sheet. Can you please help to sort out the problem?
D = 'F:\MATLAB\R2018a\bin\rayed mat lab\experimental';
S = dir(fullfile(D,'*.jpg'));
for k = 1:numel(S)
F = fullfile(D,S(k).name);
I = imread(F);
ih=rgb2hsv(I);
ih1=ih(:,:,1);
binary=ih1>0.6;
leafArea = sum(binary(:))
subplot(2, 2, 2);
imshow(binary, []);
message = sprintf('The leaf area = %d pixels.\n pixels = %.1f%%',...
leafArea, defectArea/leafArea*100);
uiwait(msgbox(message))
end

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by