Comparing two arrays +

1 回表示 (過去 30 日間)
gmltn1212
gmltn1212 2020 年 5 月 29 日
コメント済み: Prasad Reddy 2020 年 5 月 30 日
Hi I am trying to compare two arrays and apply different mathematical operations here...
a = [10 20 30 40];
b = [20 30 30 10];
I want to compare first index of two arrays and if the first index of first array is bigger than the second array one, take the first value and return it
If its not, take an average of two and return.
So it will return this...
c = [15 25 30 40];
Could you help me with finding this?
  1 件のコメント
James Tursa
James Tursa 2020 年 5 月 29 日
編集済み: James Tursa 2020 年 5 月 29 日
What have you done so far? What specific problems are you having with your code? Have you been instructed to use for-loops or can you use any MATLAB functions?

サインインしてコメントする。

回答 (1 件)

Prasad Reddy
Prasad Reddy 2020 年 5 月 30 日
clc
clear all
a=[10 20 30 40];
b=[20 30 30 10];
for i=1:length(a)
if a(i)>=b(i)
c(i)=a(i);
else
c(i)=(a(i)+b(i))/2;
end
end
c
% This will work perfectly untill both a and b are of same length. please provide a up thumb if this helps to you.
% Thank you.
  4 件のコメント
madhan ravi
madhan ravi 2020 年 5 月 30 日
xD good question. If you see the comment above given by James it indicates that it is a homework question ;).
Prasad Reddy
Prasad Reddy 2020 年 5 月 30 日
Ok. Nice. But maybe he may learn even from this complete solution. It dosent gaurentee thst he will copy the answer simpley without understanding since we provided complete solution.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by