Hi, I've just tried to make 2 for loops at the same time but I couldn't achieve that properly. Assume that "A" can be any matrix. I upload example.xlsx file as an .txt file.
A = xlsread('example.xlsx');
for ii= 5:5:length(A);
for jj= 4:5:length(A);
M=A(ii,11)*(10^A(jj,1))
end
end
When I try this code I get 16 M as an answer, I want to take 4 answers. Does anybody have any clue that can help ?

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 19 Mai 2018

2 Stimmen

A = xlsread('example.xlsx');
iivals = 5:5:length(A);
jjvals = 4:5:length(A);
numii = length(iivals);
numjj = length(jjvals);
M = zeros(numii, numjj);
for ii_idx = 1 : numii
ii = iivals(ii_idx);
for jj_idx = 1 : numjj
jj = jjvals(jj_idx);
M(ii_idx, jj_idx) = A(ii,11)*(10^A(jj,1))
end
end

3 Kommentare

Gökçe Öter
Gökçe Öter am 19 Mai 2018
Thanks for your effort and quick answer also but with this code i get 16 matrix answers, I just want to get 4 value as an answer which are multiplication of two elements of A matrix.
A = xlsread('example.xlsx');
for ii= 5:5:length(A);
M( ii/5 ) = A(ii,11) * (10^A(ii-1,1))
end
Gökçe Öter
Gökçe Öter am 19 Mai 2018
Thank you so much !

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (3)

Majid Farzaneh
Majid Farzaneh am 19 Mai 2018
Bearbeitet: Majid Farzaneh am 19 Mai 2018

2 Stimmen

Hi, It's better use size(A,1) [for number of rows] or size(A,2) [for number of columns] instead of length(A). Because it's may not exactly what you want. At the M=A(ii,11)*(10^A(jj,1)) if you want to multiply values element by element you should use '.*' instead of '*'.
for ii= 5:5:size(A,1);
for jj= 4:5:size(A,1);
M=A(ii,11).*(10^A(jj,1))
end
end

1 Kommentar

Thanks for your effort, but with this code i get 16 matrix answers, I just want to get 4 value as an answer
0.1312*1.0e+24 0.3681*1.0e+24 0.6380*1.0e+24 0.4334*1.0e+24
which are multiplication of two elements of A matrix.

Melden Sie sich an, um zu kommentieren.

Rik
Rik am 19 Mai 2018

1 Stimme

You must index M.
jj_= 4:5:length(A);
ii_= 5:5:length(A);
A = xlsread('example.xlsx');
M=zeros(numel(ii),numel(jj));%pre-allocate
for ii=1:numel(ii_)
for jj=1:numel(jj_)
M(ii,jj)=A(ii_(ii),11)*(10^A(jj_(jj),1));
end
end
Ameer Hamza
Ameer Hamza am 19 Mai 2018
Bearbeitet: Ameer Hamza am 19 Mai 2018

1 Stimme

You don't need a for loop. Replace it with this:
M = A(5:5:length(A), 11).*10.^(A(4:5:length(A), 1));
This will work in R2016b and later.

4 Kommentare

Thank you so much, but this code gives
1.0e+24 *
0.1312 0.1312 0.1312 1.3120
0.3681 0.3681 0.3681 3.6810
0.6380 0.6380 0.6380 6.3800
0.4334 0.4334 0.4334 4.3340
But I want to get an answer as
0.1312*1.0e+24 0.3681*1.0e+24 0.6380*1.0e+24 0.4334*1.0e+24
Ameer Hamza
Ameer Hamza am 19 Mai 2018
Since you didn't give the dataset so it is hard to see what you want. But try the updated statement.
Gökçe Öter
Gökçe Öter am 19 Mai 2018
My data set is messed up but I upload this if you want to look at it.
As you can see, that your final answer is the first column of of 4x4 matrix. So you can get it like this
M = A(5:5:length(A), 11)*10.^(A(4:5:length(A), 1))';
M = M(:,1);
It will give you your required 4 numbers

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2017b

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by